stmmac_pci.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*******************************************************************************
  2. This contains the functions to handle the pci driver.
  3. Copyright (C) 2011-2012 Vayavya Labs Pvt Ltd
  4. This program is free software; you can redistribute it and/or modify it
  5. under the terms and conditions of the GNU General Public License,
  6. version 2, as published by the Free Software Foundation.
  7. This program is distributed in the hope it will be useful, but WITHOUT
  8. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  9. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  10. more details.
  11. The full GNU General Public License is included in this distribution in
  12. the file called "COPYING".
  13. Author: Rayagond Kokatanur <rayagond@vayavyalabs.com>
  14. Author: Giuseppe Cavallaro <peppe.cavallaro@st.com>
  15. *******************************************************************************/
  16. #include <linux/pci.h>
  17. #include <linux/dmi.h>
  18. #include "stmmac.h"
  19. /*
  20. * This struct is used to associate PCI Function of MAC controller on a board,
  21. * discovered via DMI, with the address of PHY connected to the MAC. The
  22. * negative value of the address means that MAC controller is not connected
  23. * with PHY.
  24. */
  25. struct stmmac_pci_func_data {
  26. unsigned int func;
  27. int phy_addr;
  28. };
  29. struct stmmac_pci_dmi_data {
  30. const struct stmmac_pci_func_data *func;
  31. size_t nfuncs;
  32. };
  33. struct stmmac_pci_info {
  34. int (*setup)(struct pci_dev *pdev, struct plat_stmmacenet_data *plat);
  35. };
  36. static int stmmac_pci_find_phy_addr(struct pci_dev *pdev,
  37. const struct dmi_system_id *dmi_list)
  38. {
  39. const struct stmmac_pci_func_data *func_data;
  40. const struct stmmac_pci_dmi_data *dmi_data;
  41. const struct dmi_system_id *dmi_id;
  42. int func = PCI_FUNC(pdev->devfn);
  43. size_t n;
  44. dmi_id = dmi_first_match(dmi_list);
  45. if (!dmi_id)
  46. return -ENODEV;
  47. dmi_data = dmi_id->driver_data;
  48. func_data = dmi_data->func;
  49. for (n = 0; n < dmi_data->nfuncs; n++, func_data++)
  50. if (func_data->func == func)
  51. return func_data->phy_addr;
  52. return -ENODEV;
  53. }
  54. static void common_default_data(struct plat_stmmacenet_data *plat)
  55. {
  56. plat->clk_csr = 2; /* clk_csr_i = 20-35MHz & MDC = clk_csr_i/16 */
  57. plat->has_gmac = 1;
  58. plat->force_sf_dma_mode = 1;
  59. plat->mdio_bus_data->phy_reset = NULL;
  60. plat->mdio_bus_data->phy_mask = 0;
  61. /* Set default value for multicast hash bins */
  62. plat->multicast_filter_bins = HASH_TABLE_SIZE;
  63. /* Set default value for unicast filter entries */
  64. plat->unicast_filter_entries = 1;
  65. /* Set the maxmtu to a default of JUMBO_LEN */
  66. plat->maxmtu = JUMBO_LEN;
  67. /* Set default number of RX and TX queues to use */
  68. plat->tx_queues_to_use = 1;
  69. plat->rx_queues_to_use = 1;
  70. /* Disable Priority config by default */
  71. plat->tx_queues_cfg[0].use_prio = false;
  72. plat->rx_queues_cfg[0].use_prio = false;
  73. /* Disable RX queues routing by default */
  74. plat->rx_queues_cfg[0].pkt_route = 0x0;
  75. }
  76. static int stmmac_default_data(struct pci_dev *pdev,
  77. struct plat_stmmacenet_data *plat)
  78. {
  79. /* Set common default data first */
  80. common_default_data(plat);
  81. plat->bus_id = 1;
  82. plat->phy_addr = 0;
  83. plat->interface = PHY_INTERFACE_MODE_GMII;
  84. plat->dma_cfg->pbl = 32;
  85. plat->dma_cfg->pblx8 = true;
  86. /* TODO: AXI */
  87. return 0;
  88. }
  89. static const struct stmmac_pci_info stmmac_pci_info = {
  90. .setup = stmmac_default_data,
  91. };
  92. static const struct stmmac_pci_func_data galileo_stmmac_func_data[] = {
  93. {
  94. .func = 6,
  95. .phy_addr = 1,
  96. },
  97. };
  98. static const struct stmmac_pci_dmi_data galileo_stmmac_dmi_data = {
  99. .func = galileo_stmmac_func_data,
  100. .nfuncs = ARRAY_SIZE(galileo_stmmac_func_data),
  101. };
  102. static const struct stmmac_pci_func_data iot2040_stmmac_func_data[] = {
  103. {
  104. .func = 6,
  105. .phy_addr = 1,
  106. },
  107. {
  108. .func = 7,
  109. .phy_addr = 1,
  110. },
  111. };
  112. static const struct stmmac_pci_dmi_data iot2040_stmmac_dmi_data = {
  113. .func = iot2040_stmmac_func_data,
  114. .nfuncs = ARRAY_SIZE(iot2040_stmmac_func_data),
  115. };
  116. static const struct dmi_system_id quark_pci_dmi[] = {
  117. {
  118. .matches = {
  119. DMI_EXACT_MATCH(DMI_BOARD_NAME, "Galileo"),
  120. },
  121. .driver_data = (void *)&galileo_stmmac_dmi_data,
  122. },
  123. {
  124. .matches = {
  125. DMI_EXACT_MATCH(DMI_BOARD_NAME, "GalileoGen2"),
  126. },
  127. .driver_data = (void *)&galileo_stmmac_dmi_data,
  128. },
  129. /*
  130. * There are 2 types of SIMATIC IOT2000: IOT20202 and IOT2040.
  131. * The asset tag "6ES7647-0AA00-0YA2" is only for IOT2020 which
  132. * has only one pci network device while other asset tags are
  133. * for IOT2040 which has two.
  134. */
  135. {
  136. .matches = {
  137. DMI_EXACT_MATCH(DMI_BOARD_NAME, "SIMATIC IOT2000"),
  138. DMI_EXACT_MATCH(DMI_BOARD_ASSET_TAG,
  139. "6ES7647-0AA00-0YA2"),
  140. },
  141. .driver_data = (void *)&galileo_stmmac_dmi_data,
  142. },
  143. {
  144. .matches = {
  145. DMI_EXACT_MATCH(DMI_BOARD_NAME, "SIMATIC IOT2000"),
  146. },
  147. .driver_data = (void *)&iot2040_stmmac_dmi_data,
  148. },
  149. {}
  150. };
  151. static int quark_default_data(struct pci_dev *pdev,
  152. struct plat_stmmacenet_data *plat)
  153. {
  154. int ret;
  155. /* Set common default data first */
  156. common_default_data(plat);
  157. /*
  158. * Refuse to load the driver and register net device if MAC controller
  159. * does not connect to any PHY interface.
  160. */
  161. ret = stmmac_pci_find_phy_addr(pdev, quark_pci_dmi);
  162. if (ret < 0) {
  163. /* Return error to the caller on DMI enabled boards. */
  164. if (dmi_get_system_info(DMI_BOARD_NAME))
  165. return ret;
  166. /*
  167. * Galileo boards with old firmware don't support DMI. We always
  168. * use 1 here as PHY address, so at least the first found MAC
  169. * controller would be probed.
  170. */
  171. ret = 1;
  172. }
  173. plat->bus_id = PCI_DEVID(pdev->bus->number, pdev->devfn);
  174. plat->phy_addr = ret;
  175. plat->interface = PHY_INTERFACE_MODE_RMII;
  176. plat->dma_cfg->pbl = 16;
  177. plat->dma_cfg->pblx8 = true;
  178. plat->dma_cfg->fixed_burst = 1;
  179. /* AXI (TODO) */
  180. return 0;
  181. }
  182. static const struct stmmac_pci_info quark_pci_info = {
  183. .setup = quark_default_data,
  184. };
  185. /**
  186. * stmmac_pci_probe
  187. *
  188. * @pdev: pci device pointer
  189. * @id: pointer to table of device id/id's.
  190. *
  191. * Description: This probing function gets called for all PCI devices which
  192. * match the ID table and are not "owned" by other driver yet. This function
  193. * gets passed a "struct pci_dev *" for each device whose entry in the ID table
  194. * matches the device. The probe functions returns zero when the driver choose
  195. * to take "ownership" of the device or an error code(-ve no) otherwise.
  196. */
  197. static int stmmac_pci_probe(struct pci_dev *pdev,
  198. const struct pci_device_id *id)
  199. {
  200. struct stmmac_pci_info *info = (struct stmmac_pci_info *)id->driver_data;
  201. struct plat_stmmacenet_data *plat;
  202. struct stmmac_resources res;
  203. int i;
  204. int ret;
  205. plat = devm_kzalloc(&pdev->dev, sizeof(*plat), GFP_KERNEL);
  206. if (!plat)
  207. return -ENOMEM;
  208. plat->mdio_bus_data = devm_kzalloc(&pdev->dev,
  209. sizeof(*plat->mdio_bus_data),
  210. GFP_KERNEL);
  211. if (!plat->mdio_bus_data)
  212. return -ENOMEM;
  213. plat->dma_cfg = devm_kzalloc(&pdev->dev, sizeof(*plat->dma_cfg),
  214. GFP_KERNEL);
  215. if (!plat->dma_cfg)
  216. return -ENOMEM;
  217. /* Enable pci device */
  218. ret = pci_enable_device(pdev);
  219. if (ret) {
  220. dev_err(&pdev->dev, "%s: ERROR: failed to enable device\n",
  221. __func__);
  222. return ret;
  223. }
  224. /* Get the base address of device */
  225. for (i = 0; i <= PCI_STD_RESOURCE_END; i++) {
  226. if (pci_resource_len(pdev, i) == 0)
  227. continue;
  228. ret = pcim_iomap_regions(pdev, BIT(i), pci_name(pdev));
  229. if (ret)
  230. return ret;
  231. break;
  232. }
  233. pci_set_master(pdev);
  234. ret = info->setup(pdev, plat);
  235. if (ret)
  236. return ret;
  237. pci_enable_msi(pdev);
  238. memset(&res, 0, sizeof(res));
  239. res.addr = pcim_iomap_table(pdev)[i];
  240. res.wol_irq = pdev->irq;
  241. res.irq = pdev->irq;
  242. return stmmac_dvr_probe(&pdev->dev, plat, &res);
  243. }
  244. /**
  245. * stmmac_pci_remove
  246. *
  247. * @pdev: platform device pointer
  248. * Description: this function calls the main to free the net resources
  249. * and releases the PCI resources.
  250. */
  251. static void stmmac_pci_remove(struct pci_dev *pdev)
  252. {
  253. int i;
  254. stmmac_dvr_remove(&pdev->dev);
  255. for (i = 0; i <= PCI_STD_RESOURCE_END; i++) {
  256. if (pci_resource_len(pdev, i) == 0)
  257. continue;
  258. pcim_iounmap_regions(pdev, BIT(i));
  259. break;
  260. }
  261. pci_disable_device(pdev);
  262. }
  263. static int __maybe_unused stmmac_pci_suspend(struct device *dev)
  264. {
  265. struct pci_dev *pdev = to_pci_dev(dev);
  266. int ret;
  267. ret = stmmac_suspend(dev);
  268. if (ret)
  269. return ret;
  270. ret = pci_save_state(pdev);
  271. if (ret)
  272. return ret;
  273. pci_disable_device(pdev);
  274. pci_wake_from_d3(pdev, true);
  275. return 0;
  276. }
  277. static int __maybe_unused stmmac_pci_resume(struct device *dev)
  278. {
  279. struct pci_dev *pdev = to_pci_dev(dev);
  280. int ret;
  281. pci_restore_state(pdev);
  282. pci_set_power_state(pdev, PCI_D0);
  283. ret = pci_enable_device(pdev);
  284. if (ret)
  285. return ret;
  286. pci_set_master(pdev);
  287. return stmmac_resume(dev);
  288. }
  289. static SIMPLE_DEV_PM_OPS(stmmac_pm_ops, stmmac_pci_suspend, stmmac_pci_resume);
  290. /* synthetic ID, no official vendor */
  291. #define PCI_VENDOR_ID_STMMAC 0x700
  292. #define STMMAC_QUARK_ID 0x0937
  293. #define STMMAC_DEVICE_ID 0x1108
  294. #define STMMAC_DEVICE(vendor_id, dev_id, info) { \
  295. PCI_VDEVICE(vendor_id, dev_id), \
  296. .driver_data = (kernel_ulong_t)&info \
  297. }
  298. static const struct pci_device_id stmmac_id_table[] = {
  299. STMMAC_DEVICE(STMMAC, STMMAC_DEVICE_ID, stmmac_pci_info),
  300. STMMAC_DEVICE(STMICRO, PCI_DEVICE_ID_STMICRO_MAC, stmmac_pci_info),
  301. STMMAC_DEVICE(INTEL, STMMAC_QUARK_ID, quark_pci_info),
  302. {}
  303. };
  304. MODULE_DEVICE_TABLE(pci, stmmac_id_table);
  305. static struct pci_driver stmmac_pci_driver = {
  306. .name = STMMAC_RESOURCE_NAME,
  307. .id_table = stmmac_id_table,
  308. .probe = stmmac_pci_probe,
  309. .remove = stmmac_pci_remove,
  310. .driver = {
  311. .pm = &stmmac_pm_ops,
  312. },
  313. };
  314. module_pci_driver(stmmac_pci_driver);
  315. MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet PCI driver");
  316. MODULE_AUTHOR("Rayagond Kokatanur <rayagond.kokatanur@vayavyalabs.com>");
  317. MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
  318. MODULE_LICENSE("GPL");