init_ohci1394_dma.c 9.3 KB

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