Giuseppe CAVALLARO | bfab27a | 2011-12-21 03:58:19 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
| 2 | This contains the functions to handle the pci driver. |
| 3 | |
| 4 | Copyright (C) 2011-2012 Vayavya Labs Pvt Ltd |
| 5 | |
| 6 | This program is free software; you can redistribute it and/or modify it |
| 7 | under the terms and conditions of the GNU General Public License, |
| 8 | version 2, as published by the Free Software Foundation. |
| 9 | |
| 10 | This program is distributed in the hope it will be useful, but WITHOUT |
| 11 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 13 | more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License along with |
| 16 | this program; if not, write to the Free Software Foundation, Inc., |
| 17 | 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | |
| 19 | The full GNU General Public License is included in this distribution in |
| 20 | the file called "COPYING". |
| 21 | |
| 22 | Author: Rayagond Kokatanur <rayagond@vayavyalabs.com> |
| 23 | Author: Giuseppe Cavallaro <peppe.cavallaro@st.com> |
| 24 | *******************************************************************************/ |
| 25 | |
| 26 | #include <linux/pci.h> |
Andy Shevchenko | 0763d95 | 2015-01-27 21:44:48 +0200 | [diff] [blame] | 27 | #include <linux/dmi.h> |
| 28 | |
Giuseppe CAVALLARO | bfab27a | 2011-12-21 03:58:19 +0000 | [diff] [blame] | 29 | #include "stmmac.h" |
| 30 | |
Andy Shevchenko | 0763d95 | 2015-01-27 21:44:48 +0200 | [diff] [blame] | 31 | /* |
| 32 | * This struct is used to associate PCI Function of MAC controller on a board, |
| 33 | * discovered via DMI, with the address of PHY connected to the MAC. The |
| 34 | * negative value of the address means that MAC controller is not connected |
| 35 | * with PHY. |
| 36 | */ |
| 37 | struct stmmac_pci_dmi_data { |
| 38 | const char *name; |
| 39 | unsigned int func; |
| 40 | int phy_addr; |
| 41 | }; |
| 42 | |
Kweh, Hock Leong | 5b99a6b | 2015-01-27 21:44:47 +0200 | [diff] [blame] | 43 | struct stmmac_pci_info { |
| 44 | struct pci_dev *pdev; |
| 45 | int (*setup)(struct plat_stmmacenet_data *plat, |
| 46 | struct stmmac_pci_info *info); |
Andy Shevchenko | 0763d95 | 2015-01-27 21:44:48 +0200 | [diff] [blame] | 47 | struct stmmac_pci_dmi_data *dmi; |
Kweh, Hock Leong | 5b99a6b | 2015-01-27 21:44:47 +0200 | [diff] [blame] | 48 | }; |
| 49 | |
Andy Shevchenko | 0763d95 | 2015-01-27 21:44:48 +0200 | [diff] [blame] | 50 | static int stmmac_pci_find_phy_addr(struct stmmac_pci_info *info) |
| 51 | { |
| 52 | const char *name = dmi_get_system_info(DMI_BOARD_NAME); |
| 53 | unsigned int func = PCI_FUNC(info->pdev->devfn); |
| 54 | struct stmmac_pci_dmi_data *dmi; |
| 55 | |
| 56 | /* |
| 57 | * Galileo boards with old firmware don't support DMI. We always return |
| 58 | * 1 here, so at least first found MAC controller would be probed. |
| 59 | */ |
| 60 | if (!name) |
| 61 | return 1; |
| 62 | |
| 63 | for (dmi = info->dmi; dmi->name && *dmi->name; dmi++) { |
| 64 | if (!strcmp(dmi->name, name) && dmi->func == func) |
| 65 | return dmi->phy_addr; |
| 66 | } |
| 67 | |
| 68 | return -ENODEV; |
| 69 | } |
| 70 | |
Andy Shevchenko | c4b2b9a | 2014-11-28 15:40:56 +0200 | [diff] [blame] | 71 | static void stmmac_default_data(struct plat_stmmacenet_data *plat) |
Giuseppe CAVALLARO | bfab27a | 2011-12-21 03:58:19 +0000 | [diff] [blame] | 72 | { |
Andy Shevchenko | c4b2b9a | 2014-11-28 15:40:56 +0200 | [diff] [blame] | 73 | plat->bus_id = 1; |
| 74 | plat->phy_addr = 0; |
| 75 | plat->interface = PHY_INTERFACE_MODE_GMII; |
| 76 | plat->clk_csr = 2; /* clk_csr_i = 20-35MHz & MDC = clk_csr_i/16 */ |
| 77 | plat->has_gmac = 1; |
| 78 | plat->force_sf_dma_mode = 1; |
Andy Shevchenko | 1e19e08 | 2014-10-31 18:28:03 +0200 | [diff] [blame] | 79 | |
Andy Shevchenko | c4b2b9a | 2014-11-28 15:40:56 +0200 | [diff] [blame] | 80 | plat->mdio_bus_data->phy_reset = NULL; |
| 81 | plat->mdio_bus_data->phy_mask = 0; |
Giuseppe CAVALLARO | bfab27a | 2011-12-21 03:58:19 +0000 | [diff] [blame] | 82 | |
Andy Shevchenko | c4b2b9a | 2014-11-28 15:40:56 +0200 | [diff] [blame] | 83 | plat->dma_cfg->pbl = 32; |
Giuseppe Cavallaro | afea036 | 2016-02-29 14:27:28 +0100 | [diff] [blame] | 84 | /* TODO: AXI */ |
Andy Shevchenko | 1e19e08 | 2014-10-31 18:28:03 +0200 | [diff] [blame] | 85 | |
| 86 | /* Set default value for multicast hash bins */ |
Andy Shevchenko | c4b2b9a | 2014-11-28 15:40:56 +0200 | [diff] [blame] | 87 | plat->multicast_filter_bins = HASH_TABLE_SIZE; |
Andy Shevchenko | 1e19e08 | 2014-10-31 18:28:03 +0200 | [diff] [blame] | 88 | |
| 89 | /* Set default value for unicast filter entries */ |
Andy Shevchenko | c4b2b9a | 2014-11-28 15:40:56 +0200 | [diff] [blame] | 90 | plat->unicast_filter_entries = 1; |
Giuseppe CAVALLARO | bfab27a | 2011-12-21 03:58:19 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Kweh, Hock Leong | 5b99a6b | 2015-01-27 21:44:47 +0200 | [diff] [blame] | 93 | static int quark_default_data(struct plat_stmmacenet_data *plat, |
| 94 | struct stmmac_pci_info *info) |
| 95 | { |
| 96 | struct pci_dev *pdev = info->pdev; |
Andy Shevchenko | 0763d95 | 2015-01-27 21:44:48 +0200 | [diff] [blame] | 97 | int ret; |
| 98 | |
| 99 | /* |
| 100 | * Refuse to load the driver and register net device if MAC controller |
| 101 | * does not connect to any PHY interface. |
| 102 | */ |
| 103 | ret = stmmac_pci_find_phy_addr(info); |
| 104 | if (ret < 0) |
| 105 | return ret; |
Kweh, Hock Leong | 5b99a6b | 2015-01-27 21:44:47 +0200 | [diff] [blame] | 106 | |
| 107 | plat->bus_id = PCI_DEVID(pdev->bus->number, pdev->devfn); |
Andy Shevchenko | 0763d95 | 2015-01-27 21:44:48 +0200 | [diff] [blame] | 108 | plat->phy_addr = ret; |
Kweh, Hock Leong | 5b99a6b | 2015-01-27 21:44:47 +0200 | [diff] [blame] | 109 | plat->interface = PHY_INTERFACE_MODE_RMII; |
| 110 | plat->clk_csr = 2; |
| 111 | plat->has_gmac = 1; |
| 112 | plat->force_sf_dma_mode = 1; |
| 113 | |
| 114 | plat->mdio_bus_data->phy_reset = NULL; |
| 115 | plat->mdio_bus_data->phy_mask = 0; |
| 116 | |
| 117 | plat->dma_cfg->pbl = 16; |
Kweh, Hock Leong | 5b99a6b | 2015-01-27 21:44:47 +0200 | [diff] [blame] | 118 | plat->dma_cfg->fixed_burst = 1; |
Giuseppe Cavallaro | afea036 | 2016-02-29 14:27:28 +0100 | [diff] [blame] | 119 | /* AXI (TODO) */ |
Kweh, Hock Leong | 5b99a6b | 2015-01-27 21:44:47 +0200 | [diff] [blame] | 120 | |
| 121 | /* Set default value for multicast hash bins */ |
| 122 | plat->multicast_filter_bins = HASH_TABLE_SIZE; |
| 123 | |
| 124 | /* Set default value for unicast filter entries */ |
| 125 | plat->unicast_filter_entries = 1; |
| 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
Andy Shevchenko | 0763d95 | 2015-01-27 21:44:48 +0200 | [diff] [blame] | 130 | static struct stmmac_pci_dmi_data quark_pci_dmi_data[] = { |
| 131 | { |
| 132 | .name = "Galileo", |
| 133 | .func = 6, |
| 134 | .phy_addr = 1, |
| 135 | }, |
| 136 | { |
| 137 | .name = "GalileoGen2", |
| 138 | .func = 6, |
| 139 | .phy_addr = 1, |
| 140 | }, |
| 141 | {} |
| 142 | }; |
| 143 | |
Kweh, Hock Leong | 5b99a6b | 2015-01-27 21:44:47 +0200 | [diff] [blame] | 144 | static struct stmmac_pci_info quark_pci_info = { |
| 145 | .setup = quark_default_data, |
Andy Shevchenko | 0763d95 | 2015-01-27 21:44:48 +0200 | [diff] [blame] | 146 | .dmi = quark_pci_dmi_data, |
Kweh, Hock Leong | 5b99a6b | 2015-01-27 21:44:47 +0200 | [diff] [blame] | 147 | }; |
| 148 | |
Giuseppe CAVALLARO | bfab27a | 2011-12-21 03:58:19 +0000 | [diff] [blame] | 149 | /** |
| 150 | * stmmac_pci_probe |
| 151 | * |
| 152 | * @pdev: pci device pointer |
| 153 | * @id: pointer to table of device id/id's. |
| 154 | * |
| 155 | * Description: This probing function gets called for all PCI devices which |
| 156 | * match the ID table and are not "owned" by other driver yet. This function |
| 157 | * gets passed a "struct pci_dev *" for each device whose entry in the ID table |
| 158 | * matches the device. The probe functions returns zero when the driver choose |
| 159 | * to take "ownership" of the device or an error code(-ve no) otherwise. |
| 160 | */ |
Bill Pemberton | 979857b | 2012-12-03 09:23:34 -0500 | [diff] [blame] | 161 | static int stmmac_pci_probe(struct pci_dev *pdev, |
Greg Kroah-Hartman | 1dd06ae | 2012-12-06 14:30:56 +0000 | [diff] [blame] | 162 | const struct pci_device_id *id) |
Giuseppe CAVALLARO | bfab27a | 2011-12-21 03:58:19 +0000 | [diff] [blame] | 163 | { |
Kweh, Hock Leong | 5b99a6b | 2015-01-27 21:44:47 +0200 | [diff] [blame] | 164 | struct stmmac_pci_info *info = (struct stmmac_pci_info *)id->driver_data; |
Andy Shevchenko | c4b2b9a | 2014-11-28 15:40:56 +0200 | [diff] [blame] | 165 | struct plat_stmmacenet_data *plat; |
Joachim Eastwood | e56788c | 2015-05-20 20:03:07 +0200 | [diff] [blame] | 166 | struct stmmac_resources res; |
Giuseppe CAVALLARO | bfab27a | 2011-12-21 03:58:19 +0000 | [diff] [blame] | 167 | int i; |
Andy Shevchenko | 2a3e8e9 | 2014-11-05 12:27:28 +0200 | [diff] [blame] | 168 | int ret; |
Giuseppe CAVALLARO | bfab27a | 2011-12-21 03:58:19 +0000 | [diff] [blame] | 169 | |
Andy Shevchenko | c4b2b9a | 2014-11-28 15:40:56 +0200 | [diff] [blame] | 170 | plat = devm_kzalloc(&pdev->dev, sizeof(*plat), GFP_KERNEL); |
| 171 | if (!plat) |
| 172 | return -ENOMEM; |
| 173 | |
| 174 | plat->mdio_bus_data = devm_kzalloc(&pdev->dev, |
| 175 | sizeof(*plat->mdio_bus_data), |
| 176 | GFP_KERNEL); |
| 177 | if (!plat->mdio_bus_data) |
| 178 | return -ENOMEM; |
| 179 | |
| 180 | plat->dma_cfg = devm_kzalloc(&pdev->dev, sizeof(*plat->dma_cfg), |
| 181 | GFP_KERNEL); |
| 182 | if (!plat->dma_cfg) |
| 183 | return -ENOMEM; |
| 184 | |
Giuseppe CAVALLARO | bfab27a | 2011-12-21 03:58:19 +0000 | [diff] [blame] | 185 | /* Enable pci device */ |
Andy Shevchenko | 2a3e8e9 | 2014-11-05 12:27:28 +0200 | [diff] [blame] | 186 | ret = pcim_enable_device(pdev); |
Giuseppe CAVALLARO | bfab27a | 2011-12-21 03:58:19 +0000 | [diff] [blame] | 187 | if (ret) { |
Andy Shevchenko | 7627fc0 | 2014-11-05 12:27:29 +0200 | [diff] [blame] | 188 | dev_err(&pdev->dev, "%s: ERROR: failed to enable device\n", |
| 189 | __func__); |
Giuseppe CAVALLARO | bfab27a | 2011-12-21 03:58:19 +0000 | [diff] [blame] | 190 | return ret; |
| 191 | } |
Giuseppe CAVALLARO | bfab27a | 2011-12-21 03:58:19 +0000 | [diff] [blame] | 192 | |
| 193 | /* Get the base address of device */ |
Andy Shevchenko | 295f9d0 | 2014-11-05 12:27:26 +0200 | [diff] [blame] | 194 | for (i = 0; i <= PCI_STD_RESOURCE_END; i++) { |
Giuseppe CAVALLARO | bfab27a | 2011-12-21 03:58:19 +0000 | [diff] [blame] | 195 | if (pci_resource_len(pdev, i) == 0) |
| 196 | continue; |
Andy Shevchenko | 2a3e8e9 | 2014-11-05 12:27:28 +0200 | [diff] [blame] | 197 | ret = pcim_iomap_regions(pdev, BIT(i), pci_name(pdev)); |
| 198 | if (ret) |
| 199 | return ret; |
Giuseppe CAVALLARO | bfab27a | 2011-12-21 03:58:19 +0000 | [diff] [blame] | 200 | break; |
| 201 | } |
Andy Shevchenko | 2a3e8e9 | 2014-11-05 12:27:28 +0200 | [diff] [blame] | 202 | |
Giuseppe CAVALLARO | bfab27a | 2011-12-21 03:58:19 +0000 | [diff] [blame] | 203 | pci_set_master(pdev); |
| 204 | |
Kweh, Hock Leong | 5b99a6b | 2015-01-27 21:44:47 +0200 | [diff] [blame] | 205 | if (info) { |
| 206 | info->pdev = pdev; |
| 207 | if (info->setup) { |
| 208 | ret = info->setup(plat, info); |
| 209 | if (ret) |
| 210 | return ret; |
| 211 | } |
| 212 | } else |
| 213 | stmmac_default_data(plat); |
Giuseppe CAVALLARO | bfab27a | 2011-12-21 03:58:19 +0000 | [diff] [blame] | 214 | |
Kweh, Hock Leong | d2a029b | 2015-01-27 21:44:49 +0200 | [diff] [blame] | 215 | pci_enable_msi(pdev); |
| 216 | |
Joachim Eastwood | e56788c | 2015-05-20 20:03:07 +0200 | [diff] [blame] | 217 | memset(&res, 0, sizeof(res)); |
| 218 | res.addr = pcim_iomap_table(pdev)[i]; |
| 219 | res.wol_irq = pdev->irq; |
| 220 | res.irq = pdev->irq; |
| 221 | |
Joachim Eastwood | 15ffac7 | 2015-05-20 20:03:08 +0200 | [diff] [blame] | 222 | return stmmac_dvr_probe(&pdev->dev, plat, &res); |
Giuseppe CAVALLARO | bfab27a | 2011-12-21 03:58:19 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | /** |
Ben Hutchings | 49ce9c2 | 2012-07-10 10:56:00 +0000 | [diff] [blame] | 226 | * stmmac_pci_remove |
Giuseppe CAVALLARO | bfab27a | 2011-12-21 03:58:19 +0000 | [diff] [blame] | 227 | * |
| 228 | * @pdev: platform device pointer |
| 229 | * Description: this function calls the main to free the net resources |
| 230 | * and releases the PCI resources. |
| 231 | */ |
Bill Pemberton | 979857b | 2012-12-03 09:23:34 -0500 | [diff] [blame] | 232 | static void stmmac_pci_remove(struct pci_dev *pdev) |
Giuseppe CAVALLARO | bfab27a | 2011-12-21 03:58:19 +0000 | [diff] [blame] | 233 | { |
| 234 | struct net_device *ndev = pci_get_drvdata(pdev); |
Giuseppe CAVALLARO | bfab27a | 2011-12-21 03:58:19 +0000 | [diff] [blame] | 235 | |
| 236 | stmmac_dvr_remove(ndev); |
Giuseppe CAVALLARO | bfab27a | 2011-12-21 03:58:19 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Andy Shevchenko | 3be3d81 | 2014-11-05 12:27:27 +0200 | [diff] [blame] | 239 | #ifdef CONFIG_PM_SLEEP |
| 240 | static int stmmac_pci_suspend(struct device *dev) |
Giuseppe CAVALLARO | bfab27a | 2011-12-21 03:58:19 +0000 | [diff] [blame] | 241 | { |
Andy Shevchenko | 3be3d81 | 2014-11-05 12:27:27 +0200 | [diff] [blame] | 242 | struct pci_dev *pdev = to_pci_dev(dev); |
Giuseppe CAVALLARO | bfab27a | 2011-12-21 03:58:19 +0000 | [diff] [blame] | 243 | struct net_device *ndev = pci_get_drvdata(pdev); |
Giuseppe CAVALLARO | bfab27a | 2011-12-21 03:58:19 +0000 | [diff] [blame] | 244 | |
Andy Shevchenko | 3be3d81 | 2014-11-05 12:27:27 +0200 | [diff] [blame] | 245 | return stmmac_suspend(ndev); |
Giuseppe CAVALLARO | bfab27a | 2011-12-21 03:58:19 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Andy Shevchenko | 3be3d81 | 2014-11-05 12:27:27 +0200 | [diff] [blame] | 248 | static int stmmac_pci_resume(struct device *dev) |
Giuseppe CAVALLARO | bfab27a | 2011-12-21 03:58:19 +0000 | [diff] [blame] | 249 | { |
Andy Shevchenko | 3be3d81 | 2014-11-05 12:27:27 +0200 | [diff] [blame] | 250 | struct pci_dev *pdev = to_pci_dev(dev); |
Giuseppe CAVALLARO | bfab27a | 2011-12-21 03:58:19 +0000 | [diff] [blame] | 251 | struct net_device *ndev = pci_get_drvdata(pdev); |
| 252 | |
Giuseppe CAVALLARO | bfab27a | 2011-12-21 03:58:19 +0000 | [diff] [blame] | 253 | return stmmac_resume(ndev); |
| 254 | } |
| 255 | #endif |
| 256 | |
Andy Shevchenko | 3be3d81 | 2014-11-05 12:27:27 +0200 | [diff] [blame] | 257 | static SIMPLE_DEV_PM_OPS(stmmac_pm_ops, stmmac_pci_suspend, stmmac_pci_resume); |
| 258 | |
Giuseppe CAVALLARO | bfab27a | 2011-12-21 03:58:19 +0000 | [diff] [blame] | 259 | #define STMMAC_VENDOR_ID 0x700 |
Kweh, Hock Leong | 5b99a6b | 2015-01-27 21:44:47 +0200 | [diff] [blame] | 260 | #define STMMAC_QUARK_ID 0x0937 |
Giuseppe CAVALLARO | bfab27a | 2011-12-21 03:58:19 +0000 | [diff] [blame] | 261 | #define STMMAC_DEVICE_ID 0x1108 |
| 262 | |
Benoit Taine | 9baa3c3 | 2014-08-08 15:56:03 +0200 | [diff] [blame] | 263 | static const struct pci_device_id stmmac_id_table[] = { |
Alessandro Rubini | 5437f4b | 2012-01-23 23:08:56 +0000 | [diff] [blame] | 264 | {PCI_DEVICE(STMMAC_VENDOR_ID, STMMAC_DEVICE_ID)}, |
| 265 | {PCI_DEVICE(PCI_VENDOR_ID_STMICRO, PCI_DEVICE_ID_STMICRO_MAC)}, |
Kweh, Hock Leong | 5b99a6b | 2015-01-27 21:44:47 +0200 | [diff] [blame] | 266 | {PCI_VDEVICE(INTEL, STMMAC_QUARK_ID), (kernel_ulong_t)&quark_pci_info}, |
Alessandro Rubini | 5437f4b | 2012-01-23 23:08:56 +0000 | [diff] [blame] | 267 | {} |
Giuseppe CAVALLARO | bfab27a | 2011-12-21 03:58:19 +0000 | [diff] [blame] | 268 | }; |
| 269 | |
| 270 | MODULE_DEVICE_TABLE(pci, stmmac_id_table); |
| 271 | |
Andy Shevchenko | b2e2f0c | 2014-11-10 12:38:59 +0200 | [diff] [blame] | 272 | static struct pci_driver stmmac_pci_driver = { |
Giuseppe CAVALLARO | bfab27a | 2011-12-21 03:58:19 +0000 | [diff] [blame] | 273 | .name = STMMAC_RESOURCE_NAME, |
| 274 | .id_table = stmmac_id_table, |
| 275 | .probe = stmmac_pci_probe, |
Bill Pemberton | 979857b | 2012-12-03 09:23:34 -0500 | [diff] [blame] | 276 | .remove = stmmac_pci_remove, |
Andy Shevchenko | 3be3d81 | 2014-11-05 12:27:27 +0200 | [diff] [blame] | 277 | .driver = { |
| 278 | .pm = &stmmac_pm_ops, |
| 279 | }, |
Giuseppe CAVALLARO | bfab27a | 2011-12-21 03:58:19 +0000 | [diff] [blame] | 280 | }; |
| 281 | |
Andy Shevchenko | b2e2f0c | 2014-11-10 12:38:59 +0200 | [diff] [blame] | 282 | module_pci_driver(stmmac_pci_driver); |
| 283 | |
Giuseppe CAVALLARO | bfab27a | 2011-12-21 03:58:19 +0000 | [diff] [blame] | 284 | MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet PCI driver"); |
| 285 | MODULE_AUTHOR("Rayagond Kokatanur <rayagond.kokatanur@vayavyalabs.com>"); |
| 286 | MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>"); |
| 287 | MODULE_LICENSE("GPL"); |