init_ohci1394_dma.c 9.6 KB

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