pcie-oxnas.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. /*
  2. * PCIe driver for PLX NAS782X SoCs
  3. *
  4. * This file is licensed under the terms of the GNU General Public
  5. * License version 2. This program is licensed "as is" without any
  6. * warranty of any kind, whether express or implied.
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/pci.h>
  10. #include <linux/clk.h>
  11. #include <linux/module.h>
  12. #include <linux/mbus.h>
  13. #include <linux/slab.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/of_address.h>
  16. #include <linux/of_pci.h>
  17. #include <linux/of_irq.h>
  18. #include <linux/of_platform.h>
  19. #include <linux/of_gpio.h>
  20. #include <linux/gpio.h>
  21. #include <linux/delay.h>
  22. #include <linux/clk.h>
  23. #include <linux/reset.h>
  24. #include <mach/iomap.h>
  25. #include <mach/hardware.h>
  26. #include <mach/utils.h>
  27. #define VERSION_ID_MAGIC 0x082510b5
  28. #define LINK_UP_TIMEOUT_SECONDS 1
  29. #define NUM_CONTROLLERS 1
  30. enum {
  31. PCIE_DEVICE_TYPE_MASK = 0x0F,
  32. PCIE_DEVICE_TYPE_ENDPOINT = 0,
  33. PCIE_DEVICE_TYPE_LEGACY_ENDPOINT = 1,
  34. PCIE_DEVICE_TYPE_ROOT = 4,
  35. PCIE_LTSSM = BIT(4),
  36. PCIE_READY_ENTR_L23 = BIT(9),
  37. PCIE_LINK_UP = BIT(11),
  38. PCIE_OBTRANS = BIT(12),
  39. };
  40. enum {
  41. HCSL_BIAS_ON = BIT(0),
  42. HCSL_PCIE_EN = BIT(1),
  43. HCSL_PCIEA_EN = BIT(2),
  44. HCSL_PCIEB_EN = BIT(3),
  45. };
  46. enum {
  47. /* pcie phy reg offset */
  48. PHY_ADDR = 0,
  49. PHY_DATA = 4,
  50. /* phy data reg bits */
  51. READ_EN = BIT(16),
  52. WRITE_EN = BIT(17),
  53. CAP_DATA = BIT(18),
  54. };
  55. /* core config registers */
  56. enum {
  57. PCI_CONFIG_VERSION_DEVICEID = 0,
  58. PCI_CONFIG_COMMAND_STATUS = 4,
  59. };
  60. /* inbound config registers */
  61. enum {
  62. IB_ADDR_XLATE_ENABLE = 0xFC,
  63. /* bits */
  64. ENABLE_IN_ADDR_TRANS = BIT(0),
  65. };
  66. /* outbound config registers, offset relative to PCIE_POM0_MEM_ADDR */
  67. enum {
  68. PCIE_POM0_MEM_ADDR = 0,
  69. PCIE_POM1_MEM_ADDR = 4,
  70. PCIE_IN0_MEM_ADDR = 8,
  71. PCIE_IN1_MEM_ADDR = 12,
  72. PCIE_IN_IO_ADDR = 16,
  73. PCIE_IN_CFG0_ADDR = 20,
  74. PCIE_IN_CFG1_ADDR = 24,
  75. PCIE_IN_MSG_ADDR = 28,
  76. PCIE_IN0_MEM_LIMIT = 32,
  77. PCIE_IN1_MEM_LIMIT = 36,
  78. PCIE_IN_IO_LIMIT = 40,
  79. PCIE_IN_CFG0_LIMIT = 44,
  80. PCIE_IN_CFG1_LIMIT = 48,
  81. PCIE_IN_MSG_LIMIT = 52,
  82. PCIE_AHB_SLAVE_CTRL = 56,
  83. PCIE_SLAVE_BE_SHIFT = 22,
  84. };
  85. #define ADDR_VAL(val) ((val) & 0xFFFF)
  86. #define DATA_VAL(val) ((val) & 0xFFFF)
  87. #define PCIE_SLAVE_BE(val) ((val) << PCIE_SLAVE_BE_SHIFT)
  88. #define PCIE_SLAVE_BE_MASK PCIE_SLAVE_BE(0xF)
  89. struct oxnas_pcie_shared {
  90. /* seems all access are serialized, no lock required */
  91. int refcount;
  92. };
  93. /* Structure representing one PCIe interfaces */
  94. struct oxnas_pcie {
  95. void __iomem *cfgbase;
  96. void __iomem *base;
  97. void __iomem *inbound;
  98. void __iomem *outbound;
  99. void __iomem *pcie_ctrl;
  100. int haslink;
  101. struct platform_device *pdev;
  102. struct resource io;
  103. struct resource cfg;
  104. struct resource pre_mem; /* prefetchable */
  105. struct resource non_mem; /* non-prefetchable */
  106. struct resource busn; /* max available bus numbers */
  107. int card_reset; /* gpio pin, optional */
  108. unsigned hcsl_en; /* hcsl pci enable bit */
  109. struct clk *clk;
  110. struct clk *busclk; /* for pcie bus, actually the PLLB */
  111. void *private_data[1];
  112. spinlock_t lock;
  113. };
  114. static struct oxnas_pcie_shared pcie_shared = {
  115. .refcount = 0,
  116. };
  117. static inline struct oxnas_pcie *sys_to_pcie(struct pci_sys_data *sys)
  118. {
  119. return sys->private_data;
  120. }
  121. static inline void set_out_lanes(struct oxnas_pcie *pcie, unsigned lanes)
  122. {
  123. oxnas_register_value_mask(pcie->outbound + PCIE_AHB_SLAVE_CTRL,
  124. PCIE_SLAVE_BE_MASK, PCIE_SLAVE_BE(lanes));
  125. wmb();
  126. }
  127. static int oxnas_pcie_link_up(struct oxnas_pcie *pcie)
  128. {
  129. unsigned long end;
  130. /* Poll for PCIE link up */
  131. end = jiffies + (LINK_UP_TIMEOUT_SECONDS * HZ);
  132. while (!time_after(jiffies, end)) {
  133. if (readl(pcie->pcie_ctrl) & PCIE_LINK_UP)
  134. return 1;
  135. }
  136. return 0;
  137. }
  138. static void __init oxnas_pcie_setup_hw(struct oxnas_pcie *pcie)
  139. {
  140. /* We won't have any inbound address translation. This allows PCI
  141. * devices to access anywhere in the AHB address map. Might be regarded
  142. * as a bit dangerous, but let's get things working before we worry
  143. * about that
  144. */
  145. oxnas_register_clear_mask(pcie->inbound + IB_ADDR_XLATE_ENABLE,
  146. ENABLE_IN_ADDR_TRANS);
  147. wmb();
  148. /*
  149. * Program outbound translation windows
  150. *
  151. * Outbound window is what is referred to as "PCI client" region in HRM
  152. *
  153. * Could use the larger alternative address space to get >>64M regions
  154. * for graphics cards etc., but will not bother at this point.
  155. *
  156. * IP bug means that AMBA window size must be a power of 2
  157. *
  158. * Set mem0 window for first 16MB of outbound window non-prefetchable
  159. * Set mem1 window for second 16MB of outbound window prefetchable
  160. * Set io window for next 16MB of outbound window
  161. * Set cfg0 for final 1MB of outbound window
  162. *
  163. * Ignore mem1, cfg1 and msg windows for now as no obvious use cases for
  164. * 820 that would need them
  165. *
  166. * Probably ideally want no offset between mem0 window start as seen by
  167. * ARM and as seen on PCI bus and get Linux to assign memory regions to
  168. * PCI devices using the same "PCI client" region start address as seen
  169. * by ARM
  170. */
  171. /* Set PCIeA mem0 region to be 1st 16MB of the 64MB PCIeA window */
  172. writel_relaxed(pcie->non_mem.start, pcie->outbound + PCIE_IN0_MEM_ADDR);
  173. writel_relaxed(pcie->non_mem.end, pcie->outbound + PCIE_IN0_MEM_LIMIT);
  174. writel_relaxed(pcie->non_mem.start, pcie->outbound + PCIE_POM0_MEM_ADDR);
  175. /* Set PCIeA mem1 region to be 2nd 16MB of the 64MB PCIeA window */
  176. writel_relaxed(pcie->pre_mem.start, pcie->outbound + PCIE_IN1_MEM_ADDR);
  177. writel_relaxed(pcie->pre_mem.end, pcie->outbound + PCIE_IN1_MEM_LIMIT);
  178. writel_relaxed(pcie->pre_mem.start, pcie->outbound + PCIE_POM1_MEM_ADDR);
  179. /* Set PCIeA io to be third 16M region of the 64MB PCIeA window*/
  180. writel_relaxed(pcie->io.start, pcie->outbound + PCIE_IN_IO_ADDR);
  181. writel_relaxed(pcie->io.end, pcie->outbound + PCIE_IN_IO_LIMIT);
  182. /* Set PCIeA cgf0 to be last 16M region of the 64MB PCIeA window*/
  183. writel_relaxed(pcie->cfg.start, pcie->outbound + PCIE_IN_CFG0_ADDR);
  184. writel_relaxed(pcie->cfg.end, pcie->outbound + PCIE_IN_CFG0_LIMIT);
  185. wmb();
  186. /* Enable outbound address translation */
  187. oxnas_register_set_mask(pcie->pcie_ctrl, PCIE_OBTRANS);
  188. wmb();
  189. /*
  190. * Program PCIe command register for core to:
  191. * enable memory space
  192. * enable bus master
  193. * enable io
  194. */
  195. writel_relaxed(7, pcie->base + PCI_CONFIG_COMMAND_STATUS);
  196. /* which is which */
  197. wmb();
  198. }
  199. static unsigned oxnas_pcie_cfg_to_offset(
  200. struct pci_sys_data *sys,
  201. unsigned char bus_number,
  202. unsigned int devfn,
  203. int where)
  204. {
  205. unsigned int function = PCI_FUNC(devfn);
  206. unsigned int slot = PCI_SLOT(devfn);
  207. unsigned char bus_number_offset;
  208. bus_number_offset = bus_number - sys->busnr;
  209. /*
  210. * We'll assume for now that the offset, function, slot, bus encoding
  211. * should map onto linear, contiguous addresses in PCIe config space,
  212. * albeit that the majority will be unused as only slot 0 is valid for
  213. * any PCIe bus and most devices have only function 0
  214. *
  215. * Could be that PCIe in fact works by not encoding the slot number into
  216. * the config space address as it's known that only slot 0 is valid.
  217. * We'll have to experiment if/when we get a PCIe switch connected to
  218. * the PCIe host
  219. */
  220. return (bus_number_offset << 20) | (slot << 15) | (function << 12) |
  221. (where & ~3);
  222. }
  223. /* PCI configuration space write function */
  224. static int oxnas_pcie_wr_conf(struct pci_bus *bus, u32 devfn,
  225. int where, int size, u32 val)
  226. {
  227. unsigned long flags;
  228. struct oxnas_pcie *pcie = sys_to_pcie(bus->sysdata);
  229. unsigned offset;
  230. u32 value;
  231. u32 lanes;
  232. /* Only a single device per bus for PCIe point-to-point links */
  233. if (PCI_SLOT(devfn) > 0)
  234. return PCIBIOS_DEVICE_NOT_FOUND;
  235. if (!pcie->haslink)
  236. return PCIBIOS_DEVICE_NOT_FOUND;
  237. offset = oxnas_pcie_cfg_to_offset(bus->sysdata, bus->number, devfn,
  238. where);
  239. value = val << (8 * (where & 3));
  240. lanes = (0xf >> (4-size)) << (where & 3);
  241. /* it race with mem and io write, but the possibility is low, normally
  242. * all config writes happens at driver initialize stage, wont interleave
  243. * with others.
  244. * and many pcie cards use dword (4bytes) access mem/io access only,
  245. * so not bother to copy that ugly work-around now. */
  246. spin_lock_irqsave(&pcie->lock, flags);
  247. set_out_lanes(pcie, lanes);
  248. writel_relaxed(value, pcie->cfgbase + offset);
  249. set_out_lanes(pcie, 0xf);
  250. spin_unlock_irqrestore(&pcie->lock, flags);
  251. return PCIBIOS_SUCCESSFUL;
  252. }
  253. /* PCI configuration space read function */
  254. static int oxnas_pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where,
  255. int size, u32 *val)
  256. {
  257. struct oxnas_pcie *pcie = sys_to_pcie(bus->sysdata);
  258. unsigned offset;
  259. u32 value;
  260. u32 left_bytes, right_bytes;
  261. /* Only a single device per bus for PCIe point-to-point links */
  262. if (PCI_SLOT(devfn) > 0) {
  263. *val = 0xffffffff;
  264. return PCIBIOS_DEVICE_NOT_FOUND;
  265. }
  266. if (!pcie->haslink) {
  267. *val = 0xffffffff;
  268. return PCIBIOS_DEVICE_NOT_FOUND;
  269. }
  270. offset = oxnas_pcie_cfg_to_offset(bus->sysdata, bus->number, devfn,
  271. where);
  272. value = readl_relaxed(pcie->cfgbase + offset);
  273. left_bytes = where & 3;
  274. right_bytes = 4 - left_bytes - size;
  275. value <<= right_bytes * 8;
  276. value >>= (left_bytes + right_bytes) * 8;
  277. *val = value;
  278. return PCIBIOS_SUCCESSFUL;
  279. }
  280. static struct pci_ops oxnas_pcie_ops = {
  281. .read = oxnas_pcie_rd_conf,
  282. .write = oxnas_pcie_wr_conf,
  283. };
  284. static int __init oxnas_pcie_setup(int nr, struct pci_sys_data *sys)
  285. {
  286. struct oxnas_pcie *pcie = sys_to_pcie(sys);
  287. pci_add_resource_offset(&sys->resources, &pcie->non_mem, sys->mem_offset);
  288. pci_add_resource_offset(&sys->resources, &pcie->pre_mem, sys->mem_offset);
  289. pci_add_resource_offset(&sys->resources, &pcie->io, sys->io_offset);
  290. pci_add_resource(&sys->resources, &pcie->busn);
  291. if (sys->busnr == 0) { /* default one */
  292. sys->busnr = pcie->busn.start;
  293. }
  294. /* do not use devm_ioremap_resource, it does not like cfg resource */
  295. pcie->cfgbase = devm_ioremap(&pcie->pdev->dev, pcie->cfg.start,
  296. resource_size(&pcie->cfg));
  297. if (!pcie->cfgbase)
  298. return -ENOMEM;
  299. oxnas_pcie_setup_hw(pcie);
  300. return 1;
  301. }
  302. static void __init oxnas_pcie_enable(struct device *dev, struct oxnas_pcie *pcie)
  303. {
  304. struct hw_pci hw;
  305. int i;
  306. memset(&hw, 0, sizeof(hw));
  307. for (i = 0; i < NUM_CONTROLLERS; i++)
  308. pcie->private_data[i] = pcie;
  309. hw.nr_controllers = NUM_CONTROLLERS;
  310. /* I think use stack pointer is a bad idea though it is valid in this case */
  311. hw.private_data = pcie->private_data;
  312. hw.setup = oxnas_pcie_setup;
  313. hw.map_irq = of_irq_parse_and_map_pci;
  314. hw.ops = &oxnas_pcie_ops;
  315. /* pass dev to maintain of tree, interrupt mapping rely on this */
  316. pci_common_init_dev(dev, &hw);
  317. }
  318. void oxnas_pcie_init_shared_hw(struct platform_device *pdev,
  319. void __iomem *phybase)
  320. {
  321. struct reset_control *rstc;
  322. int ret;
  323. /* generate clocks from HCSL buffers, shared parts */
  324. writel(HCSL_BIAS_ON|HCSL_PCIE_EN, SYS_CTRL_HCSL_CTRL);
  325. /* Ensure PCIe PHY is properly reset */
  326. rstc = reset_control_get(&pdev->dev, "phy");
  327. if (IS_ERR(rstc)) {
  328. ret = PTR_ERR(rstc);
  329. } else {
  330. ret = reset_control_reset(rstc);
  331. reset_control_put(rstc);
  332. }
  333. if (ret) {
  334. dev_err(&pdev->dev, "phy reset failed %d\n", ret);
  335. return;
  336. }
  337. /* Enable PCIe Pre-Emphasis: What these value means? */
  338. writel(ADDR_VAL(0x0014), phybase + PHY_ADDR);
  339. writel(DATA_VAL(0xce10) | CAP_DATA, phybase + PHY_DATA);
  340. writel(DATA_VAL(0xce10) | WRITE_EN, phybase + PHY_DATA);
  341. writel(ADDR_VAL(0x2004), phybase + PHY_ADDR);
  342. writel(DATA_VAL(0x82c7) | CAP_DATA, phybase + PHY_DATA);
  343. writel(DATA_VAL(0x82c7) | WRITE_EN, phybase + PHY_DATA);
  344. }
  345. static int oxnas_pcie_shared_init(struct platform_device *pdev)
  346. {
  347. if (++pcie_shared.refcount == 1) {
  348. /* we are the first */
  349. struct device_node *np = pdev->dev.of_node;
  350. void __iomem *phy = of_iomap(np, 2);
  351. if (!phy) {
  352. --pcie_shared.refcount;
  353. return -ENOMEM;
  354. }
  355. oxnas_pcie_init_shared_hw(pdev, phy);
  356. iounmap(phy);
  357. return 0;
  358. } else {
  359. return 0;
  360. }
  361. }
  362. #if 0
  363. /* maybe we will call it when enter low power state */
  364. static void oxnas_pcie_shared_deinit(struct platform_device *pdev)
  365. {
  366. if (--pcie_shared.refcount == 0) {
  367. /* no cleanup needed */;
  368. }
  369. }
  370. #endif
  371. static int __init
  372. oxnas_pcie_map_registers(struct platform_device *pdev,
  373. struct device_node *np,
  374. struct oxnas_pcie *pcie)
  375. {
  376. struct resource regs;
  377. int ret = 0;
  378. u32 outbound_ctrl_offset;
  379. u32 pcie_ctrl_offset;
  380. /* 2 is reserved for shared phy */
  381. ret = of_address_to_resource(np, 0, &regs);
  382. if (ret)
  383. return -EINVAL;
  384. pcie->base = devm_ioremap_resource(&pdev->dev, &regs);
  385. if (!pcie->base)
  386. return -ENOMEM;
  387. ret = of_address_to_resource(np, 1, &regs);
  388. if (ret)
  389. return -EINVAL;
  390. pcie->inbound = devm_ioremap_resource(&pdev->dev, &regs);
  391. if (!pcie->inbound)
  392. return -ENOMEM;
  393. if (of_property_read_u32(np, "plxtech,pcie-outbound-offset",
  394. &outbound_ctrl_offset))
  395. return -EINVAL;
  396. /* SYSCRTL is shared by too many drivers, so is mapped by board file */
  397. pcie->outbound = IOMEM(OXNAS_SYSCRTL_BASE_VA + outbound_ctrl_offset);
  398. if (of_property_read_u32(np, "plxtech,pcie-ctrl-offset",
  399. &pcie_ctrl_offset))
  400. return -EINVAL;
  401. pcie->pcie_ctrl = IOMEM(OXNAS_SYSCRTL_BASE_VA + pcie_ctrl_offset);
  402. return 0;
  403. }
  404. static int __init oxnas_pcie_init_res(struct platform_device *pdev,
  405. struct oxnas_pcie *pcie,
  406. struct device_node *np)
  407. {
  408. struct of_pci_range range;
  409. struct of_pci_range_parser parser;
  410. int ret;
  411. if (of_pci_range_parser_init(&parser, np))
  412. return -EINVAL;
  413. /* Get the I/O and memory ranges from DT */
  414. for_each_of_pci_range(&parser, &range) {
  415. unsigned long restype = range.flags & IORESOURCE_TYPE_BITS;
  416. if (restype == IORESOURCE_IO) {
  417. of_pci_range_to_resource(&range, np, &pcie->io);
  418. pcie->io.name = "I/O";
  419. }
  420. if (restype == IORESOURCE_MEM) {
  421. if (range.flags & IORESOURCE_PREFETCH) {
  422. of_pci_range_to_resource(&range, np, &pcie->pre_mem);
  423. pcie->pre_mem.name = "PRE MEM";
  424. } else {
  425. of_pci_range_to_resource(&range, np, &pcie->non_mem);
  426. pcie->non_mem.name = "NON MEM";
  427. }
  428. }
  429. if (restype == 0)
  430. of_pci_range_to_resource(&range, np, &pcie->cfg);
  431. }
  432. /* Get the bus range */
  433. ret = of_pci_parse_bus_range(np, &pcie->busn);
  434. if (ret) {
  435. dev_err(&pdev->dev, "failed to parse bus-range property: %d\n",
  436. ret);
  437. return ret;
  438. }
  439. pcie->card_reset = of_get_gpio(np, 0);
  440. if (pcie->card_reset < 0)
  441. dev_info(&pdev->dev, "card reset gpio pin not exists\n");
  442. if (of_property_read_u32(np, "plxtech,pcie-hcsl-bit", &pcie->hcsl_en))
  443. return -EINVAL;
  444. pcie->clk = of_clk_get_by_name(np, "pcie");
  445. if (IS_ERR(pcie->clk)) {
  446. return PTR_ERR(pcie->clk);
  447. }
  448. pcie->busclk = of_clk_get_by_name(np, "busclk");
  449. if (IS_ERR(pcie->busclk)) {
  450. clk_put(pcie->clk);
  451. return PTR_ERR(pcie->busclk);
  452. }
  453. return 0;
  454. }
  455. static void oxnas_pcie_init_hw(struct platform_device *pdev,
  456. struct oxnas_pcie *pcie)
  457. {
  458. u32 version_id;
  459. int ret;
  460. clk_prepare_enable(pcie->busclk);
  461. /* reset PCIe cards use hard-wired gpio pin */
  462. if (pcie->card_reset >= 0 &&
  463. !gpio_direction_output(pcie->card_reset, 0)) {
  464. wmb();
  465. mdelay(10);
  466. /* must tri-state the pin to pull it up */
  467. gpio_direction_input(pcie->card_reset);
  468. wmb();
  469. mdelay(100);
  470. }
  471. oxnas_register_set_mask(SYS_CTRL_HCSL_CTRL, BIT(pcie->hcsl_en));
  472. /* core */
  473. ret = device_reset(&pdev->dev);
  474. if (ret) {
  475. dev_err(&pdev->dev, "core reset failed %d\n", ret);
  476. return;
  477. }
  478. /* Start PCIe core clocks */
  479. clk_prepare_enable(pcie->clk);
  480. version_id = readl_relaxed(pcie->base + PCI_CONFIG_VERSION_DEVICEID);
  481. dev_info(&pdev->dev, "PCIe version/deviceID 0x%x\n", version_id);
  482. if (version_id != VERSION_ID_MAGIC) {
  483. dev_info(&pdev->dev, "PCIe controller not found\n");
  484. pcie->haslink = 0;
  485. return;
  486. }
  487. /* allow entry to L23 state */
  488. oxnas_register_set_mask(pcie->pcie_ctrl, PCIE_READY_ENTR_L23);
  489. /* Set PCIe core into RootCore mode */
  490. oxnas_register_value_mask(pcie->pcie_ctrl, PCIE_DEVICE_TYPE_MASK,
  491. PCIE_DEVICE_TYPE_ROOT);
  492. wmb();
  493. /* Bring up the PCI core */
  494. oxnas_register_set_mask(pcie->pcie_ctrl, PCIE_LTSSM);
  495. wmb();
  496. }
  497. static int __init oxnas_pcie_probe(struct platform_device *pdev)
  498. {
  499. struct oxnas_pcie *pcie;
  500. struct device_node *np = pdev->dev.of_node;
  501. int ret;
  502. pcie = devm_kzalloc(&pdev->dev, sizeof(struct oxnas_pcie),
  503. GFP_KERNEL);
  504. if (!pcie)
  505. return -ENOMEM;
  506. pcie->pdev = pdev;
  507. pcie->haslink = 1;
  508. spin_lock_init(&pcie->lock);
  509. ret = oxnas_pcie_init_res(pdev, pcie, np);
  510. if (ret)
  511. return ret;
  512. if (pcie->card_reset >= 0) {
  513. ret = gpio_request_one(pcie->card_reset, GPIOF_DIR_IN,
  514. dev_name(&pdev->dev));
  515. if (ret) {
  516. dev_err(&pdev->dev, "cannot request gpio pin %d\n",
  517. pcie->card_reset);
  518. return ret;
  519. }
  520. }
  521. ret = oxnas_pcie_map_registers(pdev, np, pcie);
  522. if (ret) {
  523. dev_err(&pdev->dev, "cannot map registers\n");
  524. goto err_free_gpio;
  525. }
  526. ret = oxnas_pcie_shared_init(pdev);
  527. if (ret)
  528. goto err_free_gpio;
  529. /* if hw not found, haslink cleared */
  530. oxnas_pcie_init_hw(pdev, pcie);
  531. if (pcie->haslink && oxnas_pcie_link_up(pcie)) {
  532. pcie->haslink = 1;
  533. dev_info(&pdev->dev, "link up\n");
  534. } else {
  535. pcie->haslink = 0;
  536. dev_info(&pdev->dev, "link down\n");
  537. }
  538. /* should we register our controller even when pcie->haslink is 0 ? */
  539. /* register the controller with framework */
  540. oxnas_pcie_enable(&pdev->dev, pcie);
  541. return 0;
  542. err_free_gpio:
  543. if (pcie->card_reset)
  544. gpio_free(pcie->card_reset);
  545. return ret;
  546. }
  547. static const struct of_device_id oxnas_pcie_of_match_table[] = {
  548. { .compatible = "plxtech,nas782x-pcie", },
  549. {},
  550. };
  551. MODULE_DEVICE_TABLE(of, oxnas_pcie_of_match_table);
  552. static struct platform_driver oxnas_pcie_driver = {
  553. .driver = {
  554. .owner = THIS_MODULE,
  555. .name = "oxnas-pcie",
  556. .of_match_table =
  557. of_match_ptr(oxnas_pcie_of_match_table),
  558. },
  559. };
  560. static int __init oxnas_pcie_init(void)
  561. {
  562. return platform_driver_probe(&oxnas_pcie_driver,
  563. oxnas_pcie_probe);
  564. }
  565. subsys_initcall(oxnas_pcie_init);
  566. MODULE_AUTHOR("Ma Haijun <mahaijuns@gmail.com>");
  567. MODULE_DESCRIPTION("NAS782x PCIe driver");
  568. MODULE_LICENSE("GPLv2");