driver_gige.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. * Sonics Silicon Backplane
  3. * Broadcom Gigabit Ethernet core driver
  4. *
  5. * Copyright 2008, Broadcom Corporation
  6. * Copyright 2008, Michael Buesch <m@bues.ch>
  7. *
  8. * Licensed under the GNU/GPL. See COPYING for details.
  9. */
  10. #include <linux/ssb/ssb.h>
  11. #include <linux/ssb/ssb_driver_gige.h>
  12. #include <linux/export.h>
  13. #include <linux/pci.h>
  14. #include <linux/pci_regs.h>
  15. #include <linux/slab.h>
  16. /*
  17. MODULE_DESCRIPTION("SSB Broadcom Gigabit Ethernet driver");
  18. MODULE_AUTHOR("Michael Buesch");
  19. MODULE_LICENSE("GPL");
  20. */
  21. static const struct ssb_device_id ssb_gige_tbl[] = {
  22. SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_ETHERNET_GBIT, SSB_ANY_REV),
  23. SSB_DEVTABLE_END
  24. };
  25. /* MODULE_DEVICE_TABLE(ssb, ssb_gige_tbl); */
  26. static inline u8 gige_read8(struct ssb_gige *dev, u16 offset)
  27. {
  28. return ssb_read8(dev->dev, offset);
  29. }
  30. static inline u16 gige_read16(struct ssb_gige *dev, u16 offset)
  31. {
  32. return ssb_read16(dev->dev, offset);
  33. }
  34. static inline u32 gige_read32(struct ssb_gige *dev, u16 offset)
  35. {
  36. return ssb_read32(dev->dev, offset);
  37. }
  38. static inline void gige_write8(struct ssb_gige *dev,
  39. u16 offset, u8 value)
  40. {
  41. ssb_write8(dev->dev, offset, value);
  42. }
  43. static inline void gige_write16(struct ssb_gige *dev,
  44. u16 offset, u16 value)
  45. {
  46. ssb_write16(dev->dev, offset, value);
  47. }
  48. static inline void gige_write32(struct ssb_gige *dev,
  49. u16 offset, u32 value)
  50. {
  51. ssb_write32(dev->dev, offset, value);
  52. }
  53. static inline
  54. u8 gige_pcicfg_read8(struct ssb_gige *dev, unsigned int offset)
  55. {
  56. BUG_ON(offset >= 256);
  57. return gige_read8(dev, SSB_GIGE_PCICFG + offset);
  58. }
  59. static inline
  60. u16 gige_pcicfg_read16(struct ssb_gige *dev, unsigned int offset)
  61. {
  62. BUG_ON(offset >= 256);
  63. return gige_read16(dev, SSB_GIGE_PCICFG + offset);
  64. }
  65. static inline
  66. u32 gige_pcicfg_read32(struct ssb_gige *dev, unsigned int offset)
  67. {
  68. BUG_ON(offset >= 256);
  69. return gige_read32(dev, SSB_GIGE_PCICFG + offset);
  70. }
  71. static inline
  72. void gige_pcicfg_write8(struct ssb_gige *dev,
  73. unsigned int offset, u8 value)
  74. {
  75. BUG_ON(offset >= 256);
  76. gige_write8(dev, SSB_GIGE_PCICFG + offset, value);
  77. }
  78. static inline
  79. void gige_pcicfg_write16(struct ssb_gige *dev,
  80. unsigned int offset, u16 value)
  81. {
  82. BUG_ON(offset >= 256);
  83. gige_write16(dev, SSB_GIGE_PCICFG + offset, value);
  84. }
  85. static inline
  86. void gige_pcicfg_write32(struct ssb_gige *dev,
  87. unsigned int offset, u32 value)
  88. {
  89. BUG_ON(offset >= 256);
  90. gige_write32(dev, SSB_GIGE_PCICFG + offset, value);
  91. }
  92. static int __devinit ssb_gige_pci_read_config(struct pci_bus *bus,
  93. unsigned int devfn, int reg,
  94. int size, u32 *val)
  95. {
  96. struct ssb_gige *dev = container_of(bus->ops, struct ssb_gige, pci_ops);
  97. unsigned long flags;
  98. if ((PCI_SLOT(devfn) > 0) || (PCI_FUNC(devfn) > 0))
  99. return PCIBIOS_DEVICE_NOT_FOUND;
  100. if (reg >= 256)
  101. return PCIBIOS_DEVICE_NOT_FOUND;
  102. spin_lock_irqsave(&dev->lock, flags);
  103. switch (size) {
  104. case 1:
  105. *val = gige_pcicfg_read8(dev, reg);
  106. break;
  107. case 2:
  108. *val = gige_pcicfg_read16(dev, reg);
  109. break;
  110. case 4:
  111. *val = gige_pcicfg_read32(dev, reg);
  112. break;
  113. default:
  114. WARN_ON(1);
  115. }
  116. spin_unlock_irqrestore(&dev->lock, flags);
  117. return PCIBIOS_SUCCESSFUL;
  118. }
  119. static int __devinit ssb_gige_pci_write_config(struct pci_bus *bus,
  120. unsigned int devfn, int reg,
  121. int size, u32 val)
  122. {
  123. struct ssb_gige *dev = container_of(bus->ops, struct ssb_gige, pci_ops);
  124. unsigned long flags;
  125. if ((PCI_SLOT(devfn) > 0) || (PCI_FUNC(devfn) > 0))
  126. return PCIBIOS_DEVICE_NOT_FOUND;
  127. if (reg >= 256)
  128. return PCIBIOS_DEVICE_NOT_FOUND;
  129. spin_lock_irqsave(&dev->lock, flags);
  130. switch (size) {
  131. case 1:
  132. gige_pcicfg_write8(dev, reg, val);
  133. break;
  134. case 2:
  135. gige_pcicfg_write16(dev, reg, val);
  136. break;
  137. case 4:
  138. gige_pcicfg_write32(dev, reg, val);
  139. break;
  140. default:
  141. WARN_ON(1);
  142. }
  143. spin_unlock_irqrestore(&dev->lock, flags);
  144. return PCIBIOS_SUCCESSFUL;
  145. }
  146. static int __devinit ssb_gige_probe(struct ssb_device *sdev,
  147. const struct ssb_device_id *id)
  148. {
  149. struct ssb_gige *dev;
  150. u32 base, tmslow, tmshigh;
  151. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  152. if (!dev)
  153. return -ENOMEM;
  154. dev->dev = sdev;
  155. spin_lock_init(&dev->lock);
  156. dev->pci_controller.pci_ops = &dev->pci_ops;
  157. dev->pci_controller.io_resource = &dev->io_resource;
  158. dev->pci_controller.mem_resource = &dev->mem_resource;
  159. dev->pci_controller.io_map_base = 0x800;
  160. dev->pci_ops.read = ssb_gige_pci_read_config;
  161. dev->pci_ops.write = ssb_gige_pci_write_config;
  162. dev->io_resource.name = SSB_GIGE_IO_RES_NAME;
  163. dev->io_resource.start = 0x800;
  164. dev->io_resource.end = 0x8FF;
  165. dev->io_resource.flags = IORESOURCE_IO | IORESOURCE_PCI_FIXED;
  166. if (!ssb_device_is_enabled(sdev))
  167. ssb_device_enable(sdev, 0);
  168. /* Setup BAR0. This is a 64k MMIO region. */
  169. base = ssb_admatch_base(ssb_read32(sdev, SSB_ADMATCH1));
  170. gige_pcicfg_write32(dev, PCI_BASE_ADDRESS_0, base);
  171. gige_pcicfg_write32(dev, PCI_BASE_ADDRESS_1, 0);
  172. dev->mem_resource.name = SSB_GIGE_MEM_RES_NAME;
  173. dev->mem_resource.start = base;
  174. dev->mem_resource.end = base + 0x10000 - 1;
  175. dev->mem_resource.flags = IORESOURCE_MEM | IORESOURCE_PCI_FIXED;
  176. /* Enable the memory region. */
  177. gige_pcicfg_write16(dev, PCI_COMMAND,
  178. gige_pcicfg_read16(dev, PCI_COMMAND)
  179. | PCI_COMMAND_MEMORY);
  180. /* Write flushing is controlled by the Flush Status Control register.
  181. * We want to flush every register write with a timeout and we want
  182. * to disable the IRQ mask while flushing to avoid concurrency.
  183. * Note that automatic write flushing does _not_ work from
  184. * an IRQ handler. The driver must flush manually by reading a register.
  185. */
  186. gige_write32(dev, SSB_GIGE_SHIM_FLUSHSTAT, 0x00000068);
  187. /* Check if we have an RGMII or GMII PHY-bus.
  188. * On RGMII do not bypass the DLLs */
  189. tmslow = ssb_read32(sdev, SSB_TMSLOW);
  190. tmshigh = ssb_read32(sdev, SSB_TMSHIGH);
  191. if (tmshigh & SSB_GIGE_TMSHIGH_RGMII) {
  192. tmslow &= ~SSB_GIGE_TMSLOW_TXBYPASS;
  193. tmslow &= ~SSB_GIGE_TMSLOW_RXBYPASS;
  194. dev->has_rgmii = 1;
  195. } else {
  196. tmslow |= SSB_GIGE_TMSLOW_TXBYPASS;
  197. tmslow |= SSB_GIGE_TMSLOW_RXBYPASS;
  198. dev->has_rgmii = 0;
  199. }
  200. tmslow |= SSB_GIGE_TMSLOW_DLLEN;
  201. ssb_write32(sdev, SSB_TMSLOW, tmslow);
  202. ssb_set_drvdata(sdev, dev);
  203. register_pci_controller(&dev->pci_controller);
  204. return 0;
  205. }
  206. bool pdev_is_ssb_gige_core(struct pci_dev *pdev)
  207. {
  208. if (!pdev->resource[0].name)
  209. return 0;
  210. return (strcmp(pdev->resource[0].name, SSB_GIGE_MEM_RES_NAME) == 0);
  211. }
  212. EXPORT_SYMBOL(pdev_is_ssb_gige_core);
  213. int ssb_gige_pcibios_plat_dev_init(struct ssb_device *sdev,
  214. struct pci_dev *pdev)
  215. {
  216. struct ssb_gige *dev = ssb_get_drvdata(sdev);
  217. struct resource *res;
  218. if (pdev->bus->ops != &dev->pci_ops) {
  219. /* The PCI device is not on this SSB GigE bridge device. */
  220. return -ENODEV;
  221. }
  222. /* Fixup the PCI resources. */
  223. res = &(pdev->resource[0]);
  224. res->flags = IORESOURCE_MEM | IORESOURCE_PCI_FIXED;
  225. res->name = dev->mem_resource.name;
  226. res->start = dev->mem_resource.start;
  227. res->end = dev->mem_resource.end;
  228. /* Fixup interrupt lines. */
  229. pdev->irq = ssb_mips_irq(sdev) + 2;
  230. pci_write_config_byte(pdev, PCI_INTERRUPT_LINE, pdev->irq);
  231. return 0;
  232. }
  233. int ssb_gige_map_irq(struct ssb_device *sdev,
  234. const struct pci_dev *pdev)
  235. {
  236. struct ssb_gige *dev = ssb_get_drvdata(sdev);
  237. if (pdev->bus->ops != &dev->pci_ops) {
  238. /* The PCI device is not on this SSB GigE bridge device. */
  239. return -ENODEV;
  240. }
  241. return ssb_mips_irq(sdev) + 2;
  242. }
  243. static struct ssb_driver ssb_gige_driver = {
  244. .name = "BCM-GigE",
  245. .id_table = ssb_gige_tbl,
  246. .probe = ssb_gige_probe,
  247. };
  248. int ssb_gige_init(void)
  249. {
  250. return ssb_driver_register(&ssb_gige_driver);
  251. }