blob: a2831773431a6c19d185b2e82e50897aaf7ef7d8 [file] [log] [blame]
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001/*******************************************************************************
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 Shevchenko0763d952015-01-27 21:44:48 +020027#include <linux/dmi.h>
28
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +000029#include "stmmac.h"
30
Andy Shevchenko0763d952015-01-27 21:44:48 +020031/*
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 */
37struct stmmac_pci_dmi_data {
38 const char *name;
39 unsigned int func;
40 int phy_addr;
41};
42
Kweh, Hock Leong5b99a6b2015-01-27 21:44:47 +020043struct stmmac_pci_info {
44 struct pci_dev *pdev;
45 int (*setup)(struct plat_stmmacenet_data *plat,
46 struct stmmac_pci_info *info);
Andy Shevchenko0763d952015-01-27 21:44:48 +020047 struct stmmac_pci_dmi_data *dmi;
Kweh, Hock Leong5b99a6b2015-01-27 21:44:47 +020048};
49
Andy Shevchenko0763d952015-01-27 21:44:48 +020050static 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 Shevchenkoc4b2b9a2014-11-28 15:40:56 +020071static void stmmac_default_data(struct plat_stmmacenet_data *plat)
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +000072{
Andy Shevchenkoc4b2b9a2014-11-28 15:40:56 +020073 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 Shevchenko1e19e082014-10-31 18:28:03 +020079
Andy Shevchenkoc4b2b9a2014-11-28 15:40:56 +020080 plat->mdio_bus_data->phy_reset = NULL;
81 plat->mdio_bus_data->phy_mask = 0;
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +000082
Andy Shevchenkoc4b2b9a2014-11-28 15:40:56 +020083 plat->dma_cfg->pbl = 32;
Niklas Cassel4022d032016-12-07 15:20:08 +010084 plat->dma_cfg->pblx8 = true;
Giuseppe Cavallaroafea0362016-02-29 14:27:28 +010085 /* TODO: AXI */
Andy Shevchenko1e19e082014-10-31 18:28:03 +020086
87 /* Set default value for multicast hash bins */
Andy Shevchenkoc4b2b9a2014-11-28 15:40:56 +020088 plat->multicast_filter_bins = HASH_TABLE_SIZE;
Andy Shevchenko1e19e082014-10-31 18:28:03 +020089
90 /* Set default value for unicast filter entries */
Andy Shevchenkoc4b2b9a2014-11-28 15:40:56 +020091 plat->unicast_filter_entries = 1;
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +000092}
93
Kweh, Hock Leong5b99a6b2015-01-27 21:44:47 +020094static int quark_default_data(struct plat_stmmacenet_data *plat,
95 struct stmmac_pci_info *info)
96{
97 struct pci_dev *pdev = info->pdev;
Andy Shevchenko0763d952015-01-27 21:44:48 +020098 int ret;
99
100 /*
101 * Refuse to load the driver and register net device if MAC controller
102 * does not connect to any PHY interface.
103 */
104 ret = stmmac_pci_find_phy_addr(info);
105 if (ret < 0)
106 return ret;
Kweh, Hock Leong5b99a6b2015-01-27 21:44:47 +0200107
108 plat->bus_id = PCI_DEVID(pdev->bus->number, pdev->devfn);
Andy Shevchenko0763d952015-01-27 21:44:48 +0200109 plat->phy_addr = ret;
Kweh, Hock Leong5b99a6b2015-01-27 21:44:47 +0200110 plat->interface = PHY_INTERFACE_MODE_RMII;
111 plat->clk_csr = 2;
112 plat->has_gmac = 1;
113 plat->force_sf_dma_mode = 1;
114
115 plat->mdio_bus_data->phy_reset = NULL;
116 plat->mdio_bus_data->phy_mask = 0;
117
118 plat->dma_cfg->pbl = 16;
Niklas Cassel4022d032016-12-07 15:20:08 +0100119 plat->dma_cfg->pblx8 = true;
Kweh, Hock Leong5b99a6b2015-01-27 21:44:47 +0200120 plat->dma_cfg->fixed_burst = 1;
Giuseppe Cavallaroafea0362016-02-29 14:27:28 +0100121 /* AXI (TODO) */
Kweh, Hock Leong5b99a6b2015-01-27 21:44:47 +0200122
123 /* Set default value for multicast hash bins */
124 plat->multicast_filter_bins = HASH_TABLE_SIZE;
125
126 /* Set default value for unicast filter entries */
127 plat->unicast_filter_entries = 1;
128
129 return 0;
130}
131
Andy Shevchenko0763d952015-01-27 21:44:48 +0200132static struct stmmac_pci_dmi_data quark_pci_dmi_data[] = {
133 {
134 .name = "Galileo",
135 .func = 6,
136 .phy_addr = 1,
137 },
138 {
139 .name = "GalileoGen2",
140 .func = 6,
141 .phy_addr = 1,
142 },
143 {}
144};
145
Kweh, Hock Leong5b99a6b2015-01-27 21:44:47 +0200146static struct stmmac_pci_info quark_pci_info = {
147 .setup = quark_default_data,
Andy Shevchenko0763d952015-01-27 21:44:48 +0200148 .dmi = quark_pci_dmi_data,
Kweh, Hock Leong5b99a6b2015-01-27 21:44:47 +0200149};
150
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000151/**
152 * stmmac_pci_probe
153 *
154 * @pdev: pci device pointer
155 * @id: pointer to table of device id/id's.
156 *
157 * Description: This probing function gets called for all PCI devices which
158 * match the ID table and are not "owned" by other driver yet. This function
159 * gets passed a "struct pci_dev *" for each device whose entry in the ID table
160 * matches the device. The probe functions returns zero when the driver choose
161 * to take "ownership" of the device or an error code(-ve no) otherwise.
162 */
Bill Pemberton979857b2012-12-03 09:23:34 -0500163static int stmmac_pci_probe(struct pci_dev *pdev,
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +0000164 const struct pci_device_id *id)
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000165{
Kweh, Hock Leong5b99a6b2015-01-27 21:44:47 +0200166 struct stmmac_pci_info *info = (struct stmmac_pci_info *)id->driver_data;
Andy Shevchenkoc4b2b9a2014-11-28 15:40:56 +0200167 struct plat_stmmacenet_data *plat;
Joachim Eastwoode56788c2015-05-20 20:03:07 +0200168 struct stmmac_resources res;
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000169 int i;
Andy Shevchenko2a3e8e92014-11-05 12:27:28 +0200170 int ret;
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000171
Andy Shevchenkoc4b2b9a2014-11-28 15:40:56 +0200172 plat = devm_kzalloc(&pdev->dev, sizeof(*plat), GFP_KERNEL);
173 if (!plat)
174 return -ENOMEM;
175
176 plat->mdio_bus_data = devm_kzalloc(&pdev->dev,
177 sizeof(*plat->mdio_bus_data),
178 GFP_KERNEL);
179 if (!plat->mdio_bus_data)
180 return -ENOMEM;
181
182 plat->dma_cfg = devm_kzalloc(&pdev->dev, sizeof(*plat->dma_cfg),
183 GFP_KERNEL);
184 if (!plat->dma_cfg)
185 return -ENOMEM;
186
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000187 /* Enable pci device */
Andy Shevchenko2a3e8e92014-11-05 12:27:28 +0200188 ret = pcim_enable_device(pdev);
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000189 if (ret) {
Andy Shevchenko7627fc02014-11-05 12:27:29 +0200190 dev_err(&pdev->dev, "%s: ERROR: failed to enable device\n",
191 __func__);
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000192 return ret;
193 }
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000194
195 /* Get the base address of device */
Andy Shevchenko295f9d02014-11-05 12:27:26 +0200196 for (i = 0; i <= PCI_STD_RESOURCE_END; i++) {
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000197 if (pci_resource_len(pdev, i) == 0)
198 continue;
Andy Shevchenko2a3e8e92014-11-05 12:27:28 +0200199 ret = pcim_iomap_regions(pdev, BIT(i), pci_name(pdev));
200 if (ret)
201 return ret;
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000202 break;
203 }
Andy Shevchenko2a3e8e92014-11-05 12:27:28 +0200204
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000205 pci_set_master(pdev);
206
Kweh, Hock Leong5b99a6b2015-01-27 21:44:47 +0200207 if (info) {
208 info->pdev = pdev;
209 if (info->setup) {
210 ret = info->setup(plat, info);
211 if (ret)
212 return ret;
213 }
214 } else
215 stmmac_default_data(plat);
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000216
Kweh, Hock Leongd2a029b2015-01-27 21:44:49 +0200217 pci_enable_msi(pdev);
218
Joachim Eastwoode56788c2015-05-20 20:03:07 +0200219 memset(&res, 0, sizeof(res));
220 res.addr = pcim_iomap_table(pdev)[i];
221 res.wol_irq = pdev->irq;
222 res.irq = pdev->irq;
223
Joachim Eastwood15ffac72015-05-20 20:03:08 +0200224 return stmmac_dvr_probe(&pdev->dev, plat, &res);
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000225}
226
227/**
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000228 * stmmac_pci_remove
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000229 *
230 * @pdev: platform device pointer
231 * Description: this function calls the main to free the net resources
232 * and releases the PCI resources.
233 */
Bill Pemberton979857b2012-12-03 09:23:34 -0500234static void stmmac_pci_remove(struct pci_dev *pdev)
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000235{
Joachim Eastwoodf4e7bd82016-05-01 22:58:19 +0200236 stmmac_dvr_remove(&pdev->dev);
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000237}
238
Joachim Eastwoodf4e7bd82016-05-01 22:58:19 +0200239static SIMPLE_DEV_PM_OPS(stmmac_pm_ops, stmmac_suspend, stmmac_resume);
Andy Shevchenko3be3d812014-11-05 12:27:27 +0200240
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000241#define STMMAC_VENDOR_ID 0x700
Kweh, Hock Leong5b99a6b2015-01-27 21:44:47 +0200242#define STMMAC_QUARK_ID 0x0937
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000243#define STMMAC_DEVICE_ID 0x1108
244
Benoit Taine9baa3c32014-08-08 15:56:03 +0200245static const struct pci_device_id stmmac_id_table[] = {
Alessandro Rubini5437f4b2012-01-23 23:08:56 +0000246 {PCI_DEVICE(STMMAC_VENDOR_ID, STMMAC_DEVICE_ID)},
247 {PCI_DEVICE(PCI_VENDOR_ID_STMICRO, PCI_DEVICE_ID_STMICRO_MAC)},
Kweh, Hock Leong5b99a6b2015-01-27 21:44:47 +0200248 {PCI_VDEVICE(INTEL, STMMAC_QUARK_ID), (kernel_ulong_t)&quark_pci_info},
Alessandro Rubini5437f4b2012-01-23 23:08:56 +0000249 {}
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000250};
251
252MODULE_DEVICE_TABLE(pci, stmmac_id_table);
253
Andy Shevchenkob2e2f0c2014-11-10 12:38:59 +0200254static struct pci_driver stmmac_pci_driver = {
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000255 .name = STMMAC_RESOURCE_NAME,
256 .id_table = stmmac_id_table,
257 .probe = stmmac_pci_probe,
Bill Pemberton979857b2012-12-03 09:23:34 -0500258 .remove = stmmac_pci_remove,
Andy Shevchenko3be3d812014-11-05 12:27:27 +0200259 .driver = {
260 .pm = &stmmac_pm_ops,
261 },
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000262};
263
Andy Shevchenkob2e2f0c2014-11-10 12:38:59 +0200264module_pci_driver(stmmac_pci_driver);
265
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000266MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet PCI driver");
267MODULE_AUTHOR("Rayagond Kokatanur <rayagond.kokatanur@vayavyalabs.com>");
268MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
269MODULE_LICENSE("GPL");