blob: cea472a7c335e561082d42827579697fa1bacbef [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
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +000015 The full GNU General Public License is included in this distribution in
16 the file called "COPYING".
17
18 Author: Rayagond Kokatanur <rayagond@vayavyalabs.com>
19 Author: Giuseppe Cavallaro <peppe.cavallaro@st.com>
20*******************************************************************************/
21
22#include <linux/pci.h>
Andy Shevchenko0763d952015-01-27 21:44:48 +020023#include <linux/dmi.h>
24
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +000025#include "stmmac.h"
26
Andy Shevchenko0763d952015-01-27 21:44:48 +020027/*
28 * This struct is used to associate PCI Function of MAC controller on a board,
29 * discovered via DMI, with the address of PHY connected to the MAC. The
30 * negative value of the address means that MAC controller is not connected
31 * with PHY.
32 */
33struct stmmac_pci_dmi_data {
34 const char *name;
35 unsigned int func;
36 int phy_addr;
37};
38
Kweh, Hock Leong5b99a6b2015-01-27 21:44:47 +020039struct stmmac_pci_info {
40 struct pci_dev *pdev;
41 int (*setup)(struct plat_stmmacenet_data *plat,
42 struct stmmac_pci_info *info);
Andy Shevchenko0763d952015-01-27 21:44:48 +020043 struct stmmac_pci_dmi_data *dmi;
Kweh, Hock Leong5b99a6b2015-01-27 21:44:47 +020044};
45
Andy Shevchenko0763d952015-01-27 21:44:48 +020046static int stmmac_pci_find_phy_addr(struct stmmac_pci_info *info)
47{
48 const char *name = dmi_get_system_info(DMI_BOARD_NAME);
49 unsigned int func = PCI_FUNC(info->pdev->devfn);
50 struct stmmac_pci_dmi_data *dmi;
51
52 /*
53 * Galileo boards with old firmware don't support DMI. We always return
54 * 1 here, so at least first found MAC controller would be probed.
55 */
56 if (!name)
57 return 1;
58
59 for (dmi = info->dmi; dmi->name && *dmi->name; dmi++) {
60 if (!strcmp(dmi->name, name) && dmi->func == func)
61 return dmi->phy_addr;
62 }
63
64 return -ENODEV;
65}
66
Andy Shevchenkoc4b2b9a2014-11-28 15:40:56 +020067static void stmmac_default_data(struct plat_stmmacenet_data *plat)
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +000068{
Andy Shevchenkoc4b2b9a2014-11-28 15:40:56 +020069 plat->bus_id = 1;
70 plat->phy_addr = 0;
71 plat->interface = PHY_INTERFACE_MODE_GMII;
72 plat->clk_csr = 2; /* clk_csr_i = 20-35MHz & MDC = clk_csr_i/16 */
73 plat->has_gmac = 1;
74 plat->force_sf_dma_mode = 1;
Andy Shevchenko1e19e082014-10-31 18:28:03 +020075
Andy Shevchenkoc4b2b9a2014-11-28 15:40:56 +020076 plat->mdio_bus_data->phy_reset = NULL;
77 plat->mdio_bus_data->phy_mask = 0;
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +000078
Andy Shevchenkoc4b2b9a2014-11-28 15:40:56 +020079 plat->dma_cfg->pbl = 32;
Niklas Cassel4022d032016-12-07 15:20:08 +010080 plat->dma_cfg->pblx8 = true;
Giuseppe Cavallaroafea0362016-02-29 14:27:28 +010081 /* TODO: AXI */
Andy Shevchenko1e19e082014-10-31 18:28:03 +020082
83 /* Set default value for multicast hash bins */
Andy Shevchenkoc4b2b9a2014-11-28 15:40:56 +020084 plat->multicast_filter_bins = HASH_TABLE_SIZE;
Andy Shevchenko1e19e082014-10-31 18:28:03 +020085
86 /* Set default value for unicast filter entries */
Andy Shevchenkoc4b2b9a2014-11-28 15:40:56 +020087 plat->unicast_filter_entries = 1;
Kweh, Hock Leonga2cd64f2017-01-07 17:32:03 +080088
89 /* Set the maxmtu to a default of JUMBO_LEN */
90 plat->maxmtu = JUMBO_LEN;
Joao Pinto26d68512017-03-13 10:07:07 +000091
92 /* Set default number of RX and TX queues to use */
93 plat->tx_queues_to_use = 1;
94 plat->rx_queues_to_use = 1;
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +000095}
96
Kweh, Hock Leong5b99a6b2015-01-27 21:44:47 +020097static int quark_default_data(struct plat_stmmacenet_data *plat,
98 struct stmmac_pci_info *info)
99{
100 struct pci_dev *pdev = info->pdev;
Andy Shevchenko0763d952015-01-27 21:44:48 +0200101 int ret;
102
103 /*
104 * Refuse to load the driver and register net device if MAC controller
105 * does not connect to any PHY interface.
106 */
107 ret = stmmac_pci_find_phy_addr(info);
108 if (ret < 0)
109 return ret;
Kweh, Hock Leong5b99a6b2015-01-27 21:44:47 +0200110
111 plat->bus_id = PCI_DEVID(pdev->bus->number, pdev->devfn);
Andy Shevchenko0763d952015-01-27 21:44:48 +0200112 plat->phy_addr = ret;
Kweh, Hock Leong5b99a6b2015-01-27 21:44:47 +0200113 plat->interface = PHY_INTERFACE_MODE_RMII;
114 plat->clk_csr = 2;
115 plat->has_gmac = 1;
116 plat->force_sf_dma_mode = 1;
117
118 plat->mdio_bus_data->phy_reset = NULL;
119 plat->mdio_bus_data->phy_mask = 0;
120
121 plat->dma_cfg->pbl = 16;
Niklas Cassel4022d032016-12-07 15:20:08 +0100122 plat->dma_cfg->pblx8 = true;
Kweh, Hock Leong5b99a6b2015-01-27 21:44:47 +0200123 plat->dma_cfg->fixed_burst = 1;
Giuseppe Cavallaroafea0362016-02-29 14:27:28 +0100124 /* AXI (TODO) */
Kweh, Hock Leong5b99a6b2015-01-27 21:44:47 +0200125
126 /* Set default value for multicast hash bins */
127 plat->multicast_filter_bins = HASH_TABLE_SIZE;
128
129 /* Set default value for unicast filter entries */
130 plat->unicast_filter_entries = 1;
131
Kweh, Hock Leonga2cd64f2017-01-07 17:32:03 +0800132 /* Set the maxmtu to a default of JUMBO_LEN */
133 plat->maxmtu = JUMBO_LEN;
134
Kweh, Hock Leong5b99a6b2015-01-27 21:44:47 +0200135 return 0;
136}
137
Andy Shevchenko0763d952015-01-27 21:44:48 +0200138static struct stmmac_pci_dmi_data quark_pci_dmi_data[] = {
139 {
140 .name = "Galileo",
141 .func = 6,
142 .phy_addr = 1,
143 },
144 {
145 .name = "GalileoGen2",
146 .func = 6,
147 .phy_addr = 1,
148 },
149 {}
150};
151
Kweh, Hock Leong5b99a6b2015-01-27 21:44:47 +0200152static struct stmmac_pci_info quark_pci_info = {
153 .setup = quark_default_data,
Andy Shevchenko0763d952015-01-27 21:44:48 +0200154 .dmi = quark_pci_dmi_data,
Kweh, Hock Leong5b99a6b2015-01-27 21:44:47 +0200155};
156
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000157/**
158 * stmmac_pci_probe
159 *
160 * @pdev: pci device pointer
161 * @id: pointer to table of device id/id's.
162 *
163 * Description: This probing function gets called for all PCI devices which
164 * match the ID table and are not "owned" by other driver yet. This function
165 * gets passed a "struct pci_dev *" for each device whose entry in the ID table
166 * matches the device. The probe functions returns zero when the driver choose
167 * to take "ownership" of the device or an error code(-ve no) otherwise.
168 */
Bill Pemberton979857b2012-12-03 09:23:34 -0500169static int stmmac_pci_probe(struct pci_dev *pdev,
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +0000170 const struct pci_device_id *id)
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000171{
Kweh, Hock Leong5b99a6b2015-01-27 21:44:47 +0200172 struct stmmac_pci_info *info = (struct stmmac_pci_info *)id->driver_data;
Andy Shevchenkoc4b2b9a2014-11-28 15:40:56 +0200173 struct plat_stmmacenet_data *plat;
Joachim Eastwoode56788c2015-05-20 20:03:07 +0200174 struct stmmac_resources res;
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000175 int i;
Andy Shevchenko2a3e8e92014-11-05 12:27:28 +0200176 int ret;
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000177
Andy Shevchenkoc4b2b9a2014-11-28 15:40:56 +0200178 plat = devm_kzalloc(&pdev->dev, sizeof(*plat), GFP_KERNEL);
179 if (!plat)
180 return -ENOMEM;
181
182 plat->mdio_bus_data = devm_kzalloc(&pdev->dev,
183 sizeof(*plat->mdio_bus_data),
184 GFP_KERNEL);
185 if (!plat->mdio_bus_data)
186 return -ENOMEM;
187
188 plat->dma_cfg = devm_kzalloc(&pdev->dev, sizeof(*plat->dma_cfg),
189 GFP_KERNEL);
190 if (!plat->dma_cfg)
191 return -ENOMEM;
192
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000193 /* Enable pci device */
Andy Shevchenko2a3e8e92014-11-05 12:27:28 +0200194 ret = pcim_enable_device(pdev);
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000195 if (ret) {
Andy Shevchenko7627fc02014-11-05 12:27:29 +0200196 dev_err(&pdev->dev, "%s: ERROR: failed to enable device\n",
197 __func__);
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000198 return ret;
199 }
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000200
201 /* Get the base address of device */
Andy Shevchenko295f9d02014-11-05 12:27:26 +0200202 for (i = 0; i <= PCI_STD_RESOURCE_END; i++) {
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000203 if (pci_resource_len(pdev, i) == 0)
204 continue;
Andy Shevchenko2a3e8e92014-11-05 12:27:28 +0200205 ret = pcim_iomap_regions(pdev, BIT(i), pci_name(pdev));
206 if (ret)
207 return ret;
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000208 break;
209 }
Andy Shevchenko2a3e8e92014-11-05 12:27:28 +0200210
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000211 pci_set_master(pdev);
212
Kweh, Hock Leong5b99a6b2015-01-27 21:44:47 +0200213 if (info) {
214 info->pdev = pdev;
215 if (info->setup) {
216 ret = info->setup(plat, info);
217 if (ret)
218 return ret;
219 }
220 } else
221 stmmac_default_data(plat);
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000222
Kweh, Hock Leongd2a029b2015-01-27 21:44:49 +0200223 pci_enable_msi(pdev);
224
Joachim Eastwoode56788c2015-05-20 20:03:07 +0200225 memset(&res, 0, sizeof(res));
226 res.addr = pcim_iomap_table(pdev)[i];
227 res.wol_irq = pdev->irq;
228 res.irq = pdev->irq;
229
Joachim Eastwood15ffac72015-05-20 20:03:08 +0200230 return stmmac_dvr_probe(&pdev->dev, plat, &res);
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000231}
232
233/**
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000234 * stmmac_pci_remove
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000235 *
236 * @pdev: platform device pointer
237 * Description: this function calls the main to free the net resources
238 * and releases the PCI resources.
239 */
Bill Pemberton979857b2012-12-03 09:23:34 -0500240static void stmmac_pci_remove(struct pci_dev *pdev)
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000241{
Joachim Eastwoodf4e7bd82016-05-01 22:58:19 +0200242 stmmac_dvr_remove(&pdev->dev);
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000243}
244
Joachim Eastwoodf4e7bd82016-05-01 22:58:19 +0200245static SIMPLE_DEV_PM_OPS(stmmac_pm_ops, stmmac_suspend, stmmac_resume);
Andy Shevchenko3be3d812014-11-05 12:27:27 +0200246
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000247#define STMMAC_VENDOR_ID 0x700
Kweh, Hock Leong5b99a6b2015-01-27 21:44:47 +0200248#define STMMAC_QUARK_ID 0x0937
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000249#define STMMAC_DEVICE_ID 0x1108
250
Benoit Taine9baa3c32014-08-08 15:56:03 +0200251static const struct pci_device_id stmmac_id_table[] = {
Alessandro Rubini5437f4b2012-01-23 23:08:56 +0000252 {PCI_DEVICE(STMMAC_VENDOR_ID, STMMAC_DEVICE_ID)},
253 {PCI_DEVICE(PCI_VENDOR_ID_STMICRO, PCI_DEVICE_ID_STMICRO_MAC)},
Kweh, Hock Leong5b99a6b2015-01-27 21:44:47 +0200254 {PCI_VDEVICE(INTEL, STMMAC_QUARK_ID), (kernel_ulong_t)&quark_pci_info},
Alessandro Rubini5437f4b2012-01-23 23:08:56 +0000255 {}
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000256};
257
258MODULE_DEVICE_TABLE(pci, stmmac_id_table);
259
Andy Shevchenkob2e2f0c2014-11-10 12:38:59 +0200260static struct pci_driver stmmac_pci_driver = {
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000261 .name = STMMAC_RESOURCE_NAME,
262 .id_table = stmmac_id_table,
263 .probe = stmmac_pci_probe,
Bill Pemberton979857b2012-12-03 09:23:34 -0500264 .remove = stmmac_pci_remove,
Andy Shevchenko3be3d812014-11-05 12:27:27 +0200265 .driver = {
266 .pm = &stmmac_pm_ops,
267 },
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000268};
269
Andy Shevchenkob2e2f0c2014-11-10 12:38:59 +0200270module_pci_driver(stmmac_pci_driver);
271
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000272MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet PCI driver");
273MODULE_AUTHOR("Rayagond Kokatanur <rayagond.kokatanur@vayavyalabs.com>");
274MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
275MODULE_LICENSE("GPL");