1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * init_ohci1394_dma.c - Initializes physical DMA on all OHCI 1394 controllers
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2006-2007 Bernhard Kaindl <bk@suse.de>
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * Derived from drivers/ieee1394/ohci1394.c and arch/x86/kernel/early-quirks.c
8*4882a593Smuzhiyun * this file has functions to:
9*4882a593Smuzhiyun * - scan the PCI very early on boot for all OHCI 1394-compliant controllers
10*4882a593Smuzhiyun * - reset and initialize them and make them join the IEEE1394 bus and
11*4882a593Smuzhiyun * - enable physical DMA on them to allow remote debugging
12*4882a593Smuzhiyun *
13*4882a593Smuzhiyun * All code and data is marked as __init and __initdata, respective as
14*4882a593Smuzhiyun * during boot, all OHCI1394 controllers may be claimed by the firewire
15*4882a593Smuzhiyun * stack and at this point, this code should not touch them anymore.
16*4882a593Smuzhiyun *
17*4882a593Smuzhiyun * To use physical DMA after the initialization of the firewire stack,
18*4882a593Smuzhiyun * be sure that the stack enables it and (re-)attach after the bus reset
19*4882a593Smuzhiyun * which may be caused by the firewire stack initialization.
20*4882a593Smuzhiyun */
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun #include <linux/delay.h>
23*4882a593Smuzhiyun #include <linux/io.h>
24*4882a593Smuzhiyun #include <linux/kernel.h>
25*4882a593Smuzhiyun #include <linux/pci.h> /* for PCI defines */
26*4882a593Smuzhiyun #include <linux/string.h>
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun #include <asm/pci-direct.h> /* for direct PCI config space access */
29*4882a593Smuzhiyun #include <asm/fixmap.h>
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun #include <linux/init_ohci1394_dma.h>
32*4882a593Smuzhiyun #include "ohci.h"
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun int __initdata init_ohci1394_dma_early;
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun struct ohci {
37*4882a593Smuzhiyun void __iomem *registers;
38*4882a593Smuzhiyun };
39*4882a593Smuzhiyun
reg_write(const struct ohci * ohci,int offset,u32 data)40*4882a593Smuzhiyun static inline void reg_write(const struct ohci *ohci, int offset, u32 data)
41*4882a593Smuzhiyun {
42*4882a593Smuzhiyun writel(data, ohci->registers + offset);
43*4882a593Smuzhiyun }
44*4882a593Smuzhiyun
reg_read(const struct ohci * ohci,int offset)45*4882a593Smuzhiyun static inline u32 reg_read(const struct ohci *ohci, int offset)
46*4882a593Smuzhiyun {
47*4882a593Smuzhiyun return readl(ohci->registers + offset);
48*4882a593Smuzhiyun }
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun #define OHCI_LOOP_COUNT 100 /* Number of loops for reg read waits */
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun /* Reads a PHY register of an OHCI-1394 controller */
get_phy_reg(struct ohci * ohci,u8 addr)53*4882a593Smuzhiyun static inline u8 __init get_phy_reg(struct ohci *ohci, u8 addr)
54*4882a593Smuzhiyun {
55*4882a593Smuzhiyun int i;
56*4882a593Smuzhiyun u32 r;
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun reg_write(ohci, OHCI1394_PhyControl, (addr << 8) | 0x00008000);
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun for (i = 0; i < OHCI_LOOP_COUNT; i++) {
61*4882a593Smuzhiyun if (reg_read(ohci, OHCI1394_PhyControl) & 0x80000000)
62*4882a593Smuzhiyun break;
63*4882a593Smuzhiyun mdelay(1);
64*4882a593Smuzhiyun }
65*4882a593Smuzhiyun r = reg_read(ohci, OHCI1394_PhyControl);
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun return (r & 0x00ff0000) >> 16;
68*4882a593Smuzhiyun }
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun /* Writes to a PHY register of an OHCI-1394 controller */
set_phy_reg(struct ohci * ohci,u8 addr,u8 data)71*4882a593Smuzhiyun static inline void __init set_phy_reg(struct ohci *ohci, u8 addr, u8 data)
72*4882a593Smuzhiyun {
73*4882a593Smuzhiyun int i;
74*4882a593Smuzhiyun
75*4882a593Smuzhiyun reg_write(ohci, OHCI1394_PhyControl, (addr << 8) | data | 0x00004000);
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun for (i = 0; i < OHCI_LOOP_COUNT; i++) {
78*4882a593Smuzhiyun if (!(reg_read(ohci, OHCI1394_PhyControl) & 0x00004000))
79*4882a593Smuzhiyun break;
80*4882a593Smuzhiyun mdelay(1);
81*4882a593Smuzhiyun }
82*4882a593Smuzhiyun }
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun /* Resets an OHCI-1394 controller (for sane state before initialization) */
init_ohci1394_soft_reset(struct ohci * ohci)85*4882a593Smuzhiyun static inline void __init init_ohci1394_soft_reset(struct ohci *ohci)
86*4882a593Smuzhiyun {
87*4882a593Smuzhiyun int i;
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun reg_write(ohci, OHCI1394_HCControlSet, OHCI1394_HCControl_softReset);
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun for (i = 0; i < OHCI_LOOP_COUNT; i++) {
92*4882a593Smuzhiyun if (!(reg_read(ohci, OHCI1394_HCControlSet)
93*4882a593Smuzhiyun & OHCI1394_HCControl_softReset))
94*4882a593Smuzhiyun break;
95*4882a593Smuzhiyun mdelay(1);
96*4882a593Smuzhiyun }
97*4882a593Smuzhiyun }
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun #define OHCI1394_MAX_AT_REQ_RETRIES 0xf
100*4882a593Smuzhiyun #define OHCI1394_MAX_AT_RESP_RETRIES 0x2
101*4882a593Smuzhiyun #define OHCI1394_MAX_PHYS_RESP_RETRIES 0x8
102*4882a593Smuzhiyun
103*4882a593Smuzhiyun /* Basic OHCI-1394 register and port inititalization */
init_ohci1394_initialize(struct ohci * ohci)104*4882a593Smuzhiyun static inline void __init init_ohci1394_initialize(struct ohci *ohci)
105*4882a593Smuzhiyun {
106*4882a593Smuzhiyun u32 bus_options;
107*4882a593Smuzhiyun int num_ports, i;
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun /* Put some defaults to these undefined bus options */
110*4882a593Smuzhiyun bus_options = reg_read(ohci, OHCI1394_BusOptions);
111*4882a593Smuzhiyun bus_options |= 0x60000000; /* Enable CMC and ISC */
112*4882a593Smuzhiyun bus_options &= ~0x00ff0000; /* XXX: Set cyc_clk_acc to zero for now */
113*4882a593Smuzhiyun bus_options &= ~0x18000000; /* Disable PMC and BMC */
114*4882a593Smuzhiyun reg_write(ohci, OHCI1394_BusOptions, bus_options);
115*4882a593Smuzhiyun
116*4882a593Smuzhiyun /* Set the bus number */
117*4882a593Smuzhiyun reg_write(ohci, OHCI1394_NodeID, 0x0000ffc0);
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun /* Enable posted writes */
120*4882a593Smuzhiyun reg_write(ohci, OHCI1394_HCControlSet,
121*4882a593Smuzhiyun OHCI1394_HCControl_postedWriteEnable);
122*4882a593Smuzhiyun
123*4882a593Smuzhiyun /* Clear link control register */
124*4882a593Smuzhiyun reg_write(ohci, OHCI1394_LinkControlClear, 0xffffffff);
125*4882a593Smuzhiyun
126*4882a593Smuzhiyun /* enable phys */
127*4882a593Smuzhiyun reg_write(ohci, OHCI1394_LinkControlSet,
128*4882a593Smuzhiyun OHCI1394_LinkControl_rcvPhyPkt);
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun /* Don't accept phy packets into AR request context */
131*4882a593Smuzhiyun reg_write(ohci, OHCI1394_LinkControlClear, 0x00000400);
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun /* Clear the Isochonouys interrupt masks */
134*4882a593Smuzhiyun reg_write(ohci, OHCI1394_IsoRecvIntMaskClear, 0xffffffff);
135*4882a593Smuzhiyun reg_write(ohci, OHCI1394_IsoRecvIntEventClear, 0xffffffff);
136*4882a593Smuzhiyun reg_write(ohci, OHCI1394_IsoXmitIntMaskClear, 0xffffffff);
137*4882a593Smuzhiyun reg_write(ohci, OHCI1394_IsoXmitIntEventClear, 0xffffffff);
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun /* Accept asynchronous transfer requests from all nodes for now */
140*4882a593Smuzhiyun reg_write(ohci, OHCI1394_AsReqFilterHiSet, 0x80000000);
141*4882a593Smuzhiyun
142*4882a593Smuzhiyun /* Specify asynchronous transfer retries */
143*4882a593Smuzhiyun reg_write(ohci, OHCI1394_ATRetries,
144*4882a593Smuzhiyun OHCI1394_MAX_AT_REQ_RETRIES |
145*4882a593Smuzhiyun (OHCI1394_MAX_AT_RESP_RETRIES<<4) |
146*4882a593Smuzhiyun (OHCI1394_MAX_PHYS_RESP_RETRIES<<8));
147*4882a593Smuzhiyun
148*4882a593Smuzhiyun /* We don't want hardware swapping */
149*4882a593Smuzhiyun reg_write(ohci, OHCI1394_HCControlClear,
150*4882a593Smuzhiyun OHCI1394_HCControl_noByteSwapData);
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun /* Enable link */
153*4882a593Smuzhiyun reg_write(ohci, OHCI1394_HCControlSet, OHCI1394_HCControl_linkEnable);
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun /* If anything is connected to a port, make sure it is enabled */
156*4882a593Smuzhiyun num_ports = get_phy_reg(ohci, 2) & 0xf;
157*4882a593Smuzhiyun for (i = 0; i < num_ports; i++) {
158*4882a593Smuzhiyun unsigned int status;
159*4882a593Smuzhiyun
160*4882a593Smuzhiyun set_phy_reg(ohci, 7, i);
161*4882a593Smuzhiyun status = get_phy_reg(ohci, 8);
162*4882a593Smuzhiyun
163*4882a593Smuzhiyun if (status & 0x20)
164*4882a593Smuzhiyun set_phy_reg(ohci, 8, status & ~1);
165*4882a593Smuzhiyun }
166*4882a593Smuzhiyun }
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun /**
169*4882a593Smuzhiyun * init_ohci1394_wait_for_busresets - wait until bus resets are completed
170*4882a593Smuzhiyun *
171*4882a593Smuzhiyun * OHCI1394 initialization itself and any device going on- or offline
172*4882a593Smuzhiyun * and any cable issue cause a IEEE1394 bus reset. The OHCI1394 spec
173*4882a593Smuzhiyun * specifies that physical DMA is disabled on each bus reset and it
174*4882a593Smuzhiyun * has to be enabled after each bus reset when needed. We resort
175*4882a593Smuzhiyun * to polling here because on early boot, we have no interrupts.
176*4882a593Smuzhiyun */
init_ohci1394_wait_for_busresets(struct ohci * ohci)177*4882a593Smuzhiyun static inline void __init init_ohci1394_wait_for_busresets(struct ohci *ohci)
178*4882a593Smuzhiyun {
179*4882a593Smuzhiyun int i, events;
180*4882a593Smuzhiyun
181*4882a593Smuzhiyun for (i = 0; i < 9; i++) {
182*4882a593Smuzhiyun mdelay(200);
183*4882a593Smuzhiyun events = reg_read(ohci, OHCI1394_IntEventSet);
184*4882a593Smuzhiyun if (events & OHCI1394_busReset)
185*4882a593Smuzhiyun reg_write(ohci, OHCI1394_IntEventClear,
186*4882a593Smuzhiyun OHCI1394_busReset);
187*4882a593Smuzhiyun }
188*4882a593Smuzhiyun }
189*4882a593Smuzhiyun
190*4882a593Smuzhiyun /**
191*4882a593Smuzhiyun * init_ohci1394_enable_physical_dma - Enable physical DMA for remote debugging
192*4882a593Smuzhiyun * This enables remote DMA access over IEEE1394 from every host for the low
193*4882a593Smuzhiyun * 4GB of address space. DMA accesses above 4GB are not available currently.
194*4882a593Smuzhiyun */
init_ohci1394_enable_physical_dma(struct ohci * ohci)195*4882a593Smuzhiyun static inline void __init init_ohci1394_enable_physical_dma(struct ohci *ohci)
196*4882a593Smuzhiyun {
197*4882a593Smuzhiyun reg_write(ohci, OHCI1394_PhyReqFilterHiSet, 0xffffffff);
198*4882a593Smuzhiyun reg_write(ohci, OHCI1394_PhyReqFilterLoSet, 0xffffffff);
199*4882a593Smuzhiyun reg_write(ohci, OHCI1394_PhyUpperBound, 0xffff0000);
200*4882a593Smuzhiyun }
201*4882a593Smuzhiyun
202*4882a593Smuzhiyun /**
203*4882a593Smuzhiyun * init_ohci1394_reset_and_init_dma - init controller and enable DMA
204*4882a593Smuzhiyun * This initializes the given controller and enables physical DMA engine in it.
205*4882a593Smuzhiyun */
init_ohci1394_reset_and_init_dma(struct ohci * ohci)206*4882a593Smuzhiyun static inline void __init init_ohci1394_reset_and_init_dma(struct ohci *ohci)
207*4882a593Smuzhiyun {
208*4882a593Smuzhiyun /* Start off with a soft reset, clears everything to a sane state. */
209*4882a593Smuzhiyun init_ohci1394_soft_reset(ohci);
210*4882a593Smuzhiyun
211*4882a593Smuzhiyun /* Accessing some registers without LPS enabled may cause lock up */
212*4882a593Smuzhiyun reg_write(ohci, OHCI1394_HCControlSet, OHCI1394_HCControl_LPS);
213*4882a593Smuzhiyun
214*4882a593Smuzhiyun /* Disable and clear interrupts */
215*4882a593Smuzhiyun reg_write(ohci, OHCI1394_IntEventClear, 0xffffffff);
216*4882a593Smuzhiyun reg_write(ohci, OHCI1394_IntMaskClear, 0xffffffff);
217*4882a593Smuzhiyun
218*4882a593Smuzhiyun mdelay(50); /* Wait 50msec to make sure we have full link enabled */
219*4882a593Smuzhiyun
220*4882a593Smuzhiyun init_ohci1394_initialize(ohci);
221*4882a593Smuzhiyun /*
222*4882a593Smuzhiyun * The initialization causes at least one IEEE1394 bus reset. Enabling
223*4882a593Smuzhiyun * physical DMA only works *after* *all* bus resets have calmed down:
224*4882a593Smuzhiyun */
225*4882a593Smuzhiyun init_ohci1394_wait_for_busresets(ohci);
226*4882a593Smuzhiyun
227*4882a593Smuzhiyun /* We had to wait and do this now if we want to debug early problems */
228*4882a593Smuzhiyun init_ohci1394_enable_physical_dma(ohci);
229*4882a593Smuzhiyun }
230*4882a593Smuzhiyun
231*4882a593Smuzhiyun /**
232*4882a593Smuzhiyun * init_ohci1394_controller - Map the registers of the controller and init DMA
233*4882a593Smuzhiyun * This maps the registers of the specified controller and initializes it
234*4882a593Smuzhiyun */
init_ohci1394_controller(int num,int slot,int func)235*4882a593Smuzhiyun static inline void __init init_ohci1394_controller(int num, int slot, int func)
236*4882a593Smuzhiyun {
237*4882a593Smuzhiyun unsigned long ohci_base;
238*4882a593Smuzhiyun struct ohci ohci;
239*4882a593Smuzhiyun
240*4882a593Smuzhiyun printk(KERN_INFO "init_ohci1394_dma: initializing OHCI-1394"
241*4882a593Smuzhiyun " at %02x:%02x.%x\n", num, slot, func);
242*4882a593Smuzhiyun
243*4882a593Smuzhiyun ohci_base = read_pci_config(num, slot, func, PCI_BASE_ADDRESS_0+(0<<2))
244*4882a593Smuzhiyun & PCI_BASE_ADDRESS_MEM_MASK;
245*4882a593Smuzhiyun
246*4882a593Smuzhiyun set_fixmap_nocache(FIX_OHCI1394_BASE, ohci_base);
247*4882a593Smuzhiyun
248*4882a593Smuzhiyun ohci.registers = (void __iomem *)fix_to_virt(FIX_OHCI1394_BASE);
249*4882a593Smuzhiyun
250*4882a593Smuzhiyun init_ohci1394_reset_and_init_dma(&ohci);
251*4882a593Smuzhiyun }
252*4882a593Smuzhiyun
253*4882a593Smuzhiyun /**
254*4882a593Smuzhiyun * debug_init_ohci1394_dma - scan for OHCI1394 controllers and init DMA on them
255*4882a593Smuzhiyun * Scans the whole PCI space for OHCI1394 controllers and inits DMA on them
256*4882a593Smuzhiyun */
init_ohci1394_dma_on_all_controllers(void)257*4882a593Smuzhiyun void __init init_ohci1394_dma_on_all_controllers(void)
258*4882a593Smuzhiyun {
259*4882a593Smuzhiyun int num, slot, func;
260*4882a593Smuzhiyun u32 class;
261*4882a593Smuzhiyun
262*4882a593Smuzhiyun if (!early_pci_allowed())
263*4882a593Smuzhiyun return;
264*4882a593Smuzhiyun
265*4882a593Smuzhiyun /* Poor man's PCI discovery, the only thing we can do at early boot */
266*4882a593Smuzhiyun for (num = 0; num < 32; num++) {
267*4882a593Smuzhiyun for (slot = 0; slot < 32; slot++) {
268*4882a593Smuzhiyun for (func = 0; func < 8; func++) {
269*4882a593Smuzhiyun class = read_pci_config(num, slot, func,
270*4882a593Smuzhiyun PCI_CLASS_REVISION);
271*4882a593Smuzhiyun if (class == 0xffffffff)
272*4882a593Smuzhiyun continue; /* No device at this func */
273*4882a593Smuzhiyun
274*4882a593Smuzhiyun if (class>>8 != PCI_CLASS_SERIAL_FIREWIRE_OHCI)
275*4882a593Smuzhiyun continue; /* Not an OHCI-1394 device */
276*4882a593Smuzhiyun
277*4882a593Smuzhiyun init_ohci1394_controller(num, slot, func);
278*4882a593Smuzhiyun break; /* Assume one controller per device */
279*4882a593Smuzhiyun }
280*4882a593Smuzhiyun }
281*4882a593Smuzhiyun }
282*4882a593Smuzhiyun printk(KERN_INFO "init_ohci1394_dma: finished initializing OHCI DMA\n");
283*4882a593Smuzhiyun }
284*4882a593Smuzhiyun
285*4882a593Smuzhiyun /**
286*4882a593Smuzhiyun * setup_init_ohci1394_early - enables early OHCI1394 DMA initialization
287*4882a593Smuzhiyun */
setup_ohci1394_dma(char * opt)288*4882a593Smuzhiyun static int __init setup_ohci1394_dma(char *opt)
289*4882a593Smuzhiyun {
290*4882a593Smuzhiyun if (!strcmp(opt, "early"))
291*4882a593Smuzhiyun init_ohci1394_dma_early = 1;
292*4882a593Smuzhiyun return 0;
293*4882a593Smuzhiyun }
294*4882a593Smuzhiyun
295*4882a593Smuzhiyun /* passing ohci1394_dma=early on boot causes early OHCI1394 DMA initialization */
296*4882a593Smuzhiyun early_param("ohci1394_dma", setup_ohci1394_dma);
297