driver_gige.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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/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 __devinit ssb_gige_pci_read_config(struct pci_bus *bus,
  92. unsigned int devfn, int reg,
  93. int size, u32 *val)
  94. {
  95. struct ssb_gige *dev = container_of(bus->ops, struct ssb_gige, pci_ops);
  96. unsigned long flags;
  97. if ((PCI_SLOT(devfn) > 0) || (PCI_FUNC(devfn) > 0))
  98. return PCIBIOS_DEVICE_NOT_FOUND;
  99. if (reg >= 256)
  100. return PCIBIOS_DEVICE_NOT_FOUND;
  101. spin_lock_irqsave(&dev->lock, flags);
  102. switch (size) {
  103. case 1:
  104. *val = gige_pcicfg_read8(dev, reg);
  105. break;
  106. case 2:
  107. *val = gige_pcicfg_read16(dev, reg);
  108. break;
  109. case 4:
  110. *val = gige_pcicfg_read32(dev, reg);
  111. break;
  112. default:
  113. WARN_ON(1);
  114. }
  115. spin_unlock_irqrestore(&dev->lock, flags);
  116. return PCIBIOS_SUCCESSFUL;
  117. }
  118. static int __devinit ssb_gige_pci_write_config(struct pci_bus *bus,
  119. unsigned int devfn, int reg,
  120. int size, u32 val)
  121. {
  122. struct ssb_gige *dev = container_of(bus->ops, struct ssb_gige, pci_ops);
  123. unsigned long flags;
  124. if ((PCI_SLOT(devfn) > 0) || (PCI_FUNC(devfn) > 0))
  125. return PCIBIOS_DEVICE_NOT_FOUND;
  126. if (reg >= 256)
  127. return PCIBIOS_DEVICE_NOT_FOUND;
  128. spin_lock_irqsave(&dev->lock, flags);
  129. switch (size) {
  130. case 1:
  131. gige_pcicfg_write8(dev, reg, val);
  132. break;
  133. case 2:
  134. gige_pcicfg_write16(dev, reg, val);
  135. break;
  136. case 4:
  137. gige_pcicfg_write32(dev, reg, val);
  138. break;
  139. default:
  140. WARN_ON(1);
  141. }
  142. spin_unlock_irqrestore(&dev->lock, flags);
  143. return PCIBIOS_SUCCESSFUL;
  144. }
  145. static int __devinit ssb_gige_probe(struct ssb_device *sdev,
  146. const struct ssb_device_id *id)
  147. {
  148. struct ssb_gige *dev;
  149. u32 base, tmslow, tmshigh;
  150. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  151. if (!dev)
  152. return -ENOMEM;
  153. dev->dev = sdev;
  154. spin_lock_init(&dev->lock);
  155. dev->pci_controller.pci_ops = &dev->pci_ops;
  156. dev->pci_controller.io_resource = &dev->io_resource;
  157. dev->pci_controller.mem_resource = &dev->mem_resource;
  158. dev->pci_controller.io_map_base = 0x800;
  159. dev->pci_ops.read = ssb_gige_pci_read_config;
  160. dev->pci_ops.write = ssb_gige_pci_write_config;
  161. dev->io_resource.name = SSB_GIGE_IO_RES_NAME;
  162. dev->io_resource.start = 0x800;
  163. dev->io_resource.end = 0x8FF;
  164. dev->io_resource.flags = IORESOURCE_IO | IORESOURCE_PCI_FIXED;
  165. if (!ssb_device_is_enabled(sdev))
  166. ssb_device_enable(sdev, 0);
  167. /* Setup BAR0. This is a 64k MMIO region. */
  168. base = ssb_admatch_base(ssb_read32(sdev, SSB_ADMATCH1));
  169. gige_pcicfg_write32(dev, PCI_BASE_ADDRESS_0, base);
  170. gige_pcicfg_write32(dev, PCI_BASE_ADDRESS_1, 0);
  171. dev->mem_resource.name = SSB_GIGE_MEM_RES_NAME;
  172. dev->mem_resource.start = base;
  173. dev->mem_resource.end = base + 0x10000 - 1;
  174. dev->mem_resource.flags = IORESOURCE_MEM | IORESOURCE_PCI_FIXED;
  175. /* Enable the memory region. */
  176. gige_pcicfg_write16(dev, PCI_COMMAND,
  177. gige_pcicfg_read16(dev, PCI_COMMAND)
  178. | PCI_COMMAND_MEMORY);
  179. /* Write flushing is controlled by the Flush Status Control register.
  180. * We want to flush every register write with a timeout and we want
  181. * to disable the IRQ mask while flushing to avoid concurrency.
  182. * Note that automatic write flushing does _not_ work from
  183. * an IRQ handler. The driver must flush manually by reading a register.
  184. */
  185. gige_write32(dev, SSB_GIGE_SHIM_FLUSHSTAT, 0x00000068);
  186. /* Check if we have an RGMII or GMII PHY-bus.
  187. * On RGMII do not bypass the DLLs */
  188. tmslow = ssb_read32(sdev, SSB_TMSLOW);
  189. tmshigh = ssb_read32(sdev, SSB_TMSHIGH);
  190. if (tmshigh & SSB_GIGE_TMSHIGH_RGMII) {
  191. tmslow &= ~SSB_GIGE_TMSLOW_TXBYPASS;
  192. tmslow &= ~SSB_GIGE_TMSLOW_RXBYPASS;
  193. dev->has_rgmii = 1;
  194. } else {
  195. tmslow |= SSB_GIGE_TMSLOW_TXBYPASS;
  196. tmslow |= SSB_GIGE_TMSLOW_RXBYPASS;
  197. dev->has_rgmii = 0;
  198. }
  199. tmslow |= SSB_GIGE_TMSLOW_DLLEN;
  200. ssb_write32(sdev, SSB_TMSLOW, tmslow);
  201. ssb_set_drvdata(sdev, dev);
  202. register_pci_controller(&dev->pci_controller);
  203. return 0;
  204. }
  205. bool pdev_is_ssb_gige_core(struct pci_dev *pdev)
  206. {
  207. if (!pdev->resource[0].name)
  208. return 0;
  209. return (strcmp(pdev->resource[0].name, SSB_GIGE_MEM_RES_NAME) == 0);
  210. }
  211. EXPORT_SYMBOL(pdev_is_ssb_gige_core);
  212. int ssb_gige_pcibios_plat_dev_init(struct ssb_device *sdev,
  213. struct pci_dev *pdev)
  214. {
  215. struct ssb_gige *dev = ssb_get_drvdata(sdev);
  216. struct resource *res;
  217. if (pdev->bus->ops != &dev->pci_ops) {
  218. /* The PCI device is not on this SSB GigE bridge device. */
  219. return -ENODEV;
  220. }
  221. /* Fixup the PCI resources. */
  222. res = &(pdev->resource[0]);
  223. res->flags = IORESOURCE_MEM | IORESOURCE_PCI_FIXED;
  224. res->name = dev->mem_resource.name;
  225. res->start = dev->mem_resource.start;
  226. res->end = dev->mem_resource.end;
  227. /* Fixup interrupt lines. */
  228. pdev->irq = ssb_mips_irq(sdev) + 2;
  229. pci_write_config_byte(pdev, PCI_INTERRUPT_LINE, pdev->irq);
  230. return 0;
  231. }
  232. int ssb_gige_map_irq(struct ssb_device *sdev,
  233. const struct pci_dev *pdev)
  234. {
  235. struct ssb_gige *dev = ssb_get_drvdata(sdev);
  236. if (pdev->bus->ops != &dev->pci_ops) {
  237. /* The PCI device is not on this SSB GigE bridge device. */
  238. return -ENODEV;
  239. }
  240. return ssb_mips_irq(sdev) + 2;
  241. }
  242. static struct ssb_driver ssb_gige_driver = {
  243. .name = "BCM-GigE",
  244. .id_table = ssb_gige_tbl,
  245. .probe = ssb_gige_probe,
  246. };
  247. int ssb_gige_init(void)
  248. {
  249. return ssb_driver_register(&ssb_gige_driver);
  250. }