driver_gige.c 7.3 KB

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