vlynq.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2006, 2007 Eugene Konev <ejka@openwrt.org>
  4. *
  5. * Parts of the VLYNQ specification can be found here:
  6. * http://www.ti.com/litv/pdf/sprue36a
  7. */
  8. #include <linux/init.h>
  9. #include <linux/types.h>
  10. #include <linux/kernel.h>
  11. #include <linux/string.h>
  12. #include <linux/device.h>
  13. #include <linux/module.h>
  14. #include <linux/errno.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/delay.h>
  18. #include <linux/io.h>
  19. #include <linux/slab.h>
  20. #include <linux/irq.h>
  21. #include <linux/vlynq.h>
  22. #define VLYNQ_CTRL_PM_ENABLE 0x80000000
  23. #define VLYNQ_CTRL_CLOCK_INT 0x00008000
  24. #define VLYNQ_CTRL_CLOCK_DIV(x) (((x) & 7) << 16)
  25. #define VLYNQ_CTRL_INT_LOCAL 0x00004000
  26. #define VLYNQ_CTRL_INT_ENABLE 0x00002000
  27. #define VLYNQ_CTRL_INT_VECTOR(x) (((x) & 0x1f) << 8)
  28. #define VLYNQ_CTRL_INT2CFG 0x00000080
  29. #define VLYNQ_CTRL_RESET 0x00000001
  30. #define VLYNQ_CTRL_CLOCK_MASK (0x7 << 16)
  31. #define VLYNQ_INT_OFFSET 0x00000014
  32. #define VLYNQ_REMOTE_OFFSET 0x00000080
  33. #define VLYNQ_STATUS_LINK 0x00000001
  34. #define VLYNQ_STATUS_LERROR 0x00000080
  35. #define VLYNQ_STATUS_RERROR 0x00000100
  36. #define VINT_ENABLE 0x00000100
  37. #define VINT_TYPE_EDGE 0x00000080
  38. #define VINT_LEVEL_LOW 0x00000040
  39. #define VINT_VECTOR(x) ((x) & 0x1f)
  40. #define VINT_OFFSET(irq) (8 * ((irq) % 4))
  41. #define VLYNQ_AUTONEGO_V2 0x00010000
  42. struct vlynq_regs {
  43. u32 revision;
  44. u32 control;
  45. u32 status;
  46. u32 int_prio;
  47. u32 int_status;
  48. u32 int_pending;
  49. u32 int_ptr;
  50. u32 tx_offset;
  51. struct vlynq_mapping rx_mapping[4];
  52. u32 chip;
  53. u32 autonego;
  54. u32 unused[6];
  55. u32 int_device[8];
  56. };
  57. #ifdef CONFIG_VLYNQ_DEBUG
  58. static void vlynq_dump_regs(struct vlynq_device *dev)
  59. {
  60. int i;
  61. printk(KERN_DEBUG "VLYNQ local=%p remote=%p\n",
  62. dev->local, dev->remote);
  63. for (i = 0; i < 32; i++) {
  64. printk(KERN_DEBUG "VLYNQ: local %d: %08x\n",
  65. i + 1, ((u32 *)dev->local)[i]);
  66. printk(KERN_DEBUG "VLYNQ: remote %d: %08x\n",
  67. i + 1, ((u32 *)dev->remote)[i]);
  68. }
  69. }
  70. static void vlynq_dump_mem(u32 *base, int count)
  71. {
  72. int i;
  73. for (i = 0; i < (count + 3) / 4; i++) {
  74. if (i % 4 == 0)
  75. printk(KERN_DEBUG "\nMEM[0x%04x]:", i * 4);
  76. printk(KERN_DEBUG " 0x%08x", *(base + i));
  77. }
  78. printk(KERN_DEBUG "\n");
  79. }
  80. #endif
  81. /* Check the VLYNQ link status with a given device */
  82. static int vlynq_linked(struct vlynq_device *dev)
  83. {
  84. int i;
  85. for (i = 0; i < 100; i++)
  86. if (readl(&dev->local->status) & VLYNQ_STATUS_LINK)
  87. return 1;
  88. else
  89. cpu_relax();
  90. return 0;
  91. }
  92. static void vlynq_reset(struct vlynq_device *dev)
  93. {
  94. writel(readl(&dev->local->control) | VLYNQ_CTRL_RESET,
  95. &dev->local->control);
  96. /* Wait for the devices to finish resetting */
  97. msleep(5);
  98. /* Remove reset bit */
  99. writel(readl(&dev->local->control) & ~VLYNQ_CTRL_RESET,
  100. &dev->local->control);
  101. /* Give some time for the devices to settle */
  102. msleep(5);
  103. }
  104. static void vlynq_irq_unmask(struct irq_data *d)
  105. {
  106. struct vlynq_device *dev = irq_data_get_irq_chip_data(d);
  107. int virq;
  108. u32 val;
  109. BUG_ON(!dev);
  110. virq = d->irq - dev->irq_start;
  111. val = readl(&dev->remote->int_device[virq >> 2]);
  112. val |= (VINT_ENABLE | virq) << VINT_OFFSET(virq);
  113. writel(val, &dev->remote->int_device[virq >> 2]);
  114. }
  115. static void vlynq_irq_mask(struct irq_data *d)
  116. {
  117. struct vlynq_device *dev = irq_data_get_irq_chip_data(d);
  118. int virq;
  119. u32 val;
  120. BUG_ON(!dev);
  121. virq = d->irq - dev->irq_start;
  122. val = readl(&dev->remote->int_device[virq >> 2]);
  123. val &= ~(VINT_ENABLE << VINT_OFFSET(virq));
  124. writel(val, &dev->remote->int_device[virq >> 2]);
  125. }
  126. static int vlynq_irq_type(struct irq_data *d, unsigned int flow_type)
  127. {
  128. struct vlynq_device *dev = irq_data_get_irq_chip_data(d);
  129. int virq;
  130. u32 val;
  131. BUG_ON(!dev);
  132. virq = d->irq - dev->irq_start;
  133. val = readl(&dev->remote->int_device[virq >> 2]);
  134. switch (flow_type & IRQ_TYPE_SENSE_MASK) {
  135. case IRQ_TYPE_EDGE_RISING:
  136. case IRQ_TYPE_EDGE_FALLING:
  137. case IRQ_TYPE_EDGE_BOTH:
  138. val |= VINT_TYPE_EDGE << VINT_OFFSET(virq);
  139. val &= ~(VINT_LEVEL_LOW << VINT_OFFSET(virq));
  140. break;
  141. case IRQ_TYPE_LEVEL_HIGH:
  142. val &= ~(VINT_TYPE_EDGE << VINT_OFFSET(virq));
  143. val &= ~(VINT_LEVEL_LOW << VINT_OFFSET(virq));
  144. break;
  145. case IRQ_TYPE_LEVEL_LOW:
  146. val &= ~(VINT_TYPE_EDGE << VINT_OFFSET(virq));
  147. val |= VINT_LEVEL_LOW << VINT_OFFSET(virq);
  148. break;
  149. default:
  150. return -EINVAL;
  151. }
  152. writel(val, &dev->remote->int_device[virq >> 2]);
  153. return 0;
  154. }
  155. static void vlynq_local_ack(struct irq_data *d)
  156. {
  157. struct vlynq_device *dev = irq_data_get_irq_chip_data(d);
  158. u32 status = readl(&dev->local->status);
  159. pr_debug("%s: local status: 0x%08x\n",
  160. dev_name(&dev->dev), status);
  161. writel(status, &dev->local->status);
  162. }
  163. static void vlynq_remote_ack(struct irq_data *d)
  164. {
  165. struct vlynq_device *dev = irq_data_get_irq_chip_data(d);
  166. u32 status = readl(&dev->remote->status);
  167. pr_debug("%s: remote status: 0x%08x\n",
  168. dev_name(&dev->dev), status);
  169. writel(status, &dev->remote->status);
  170. }
  171. static irqreturn_t vlynq_irq(int irq, void *dev_id)
  172. {
  173. struct vlynq_device *dev = dev_id;
  174. u32 status;
  175. int virq = 0;
  176. status = readl(&dev->local->int_status);
  177. writel(status, &dev->local->int_status);
  178. if (unlikely(!status))
  179. spurious_interrupt();
  180. while (status) {
  181. if (status & 1)
  182. do_IRQ(dev->irq_start + virq);
  183. status >>= 1;
  184. virq++;
  185. }
  186. return IRQ_HANDLED;
  187. }
  188. static struct irq_chip vlynq_irq_chip = {
  189. .name = "vlynq",
  190. .irq_unmask = vlynq_irq_unmask,
  191. .irq_mask = vlynq_irq_mask,
  192. .irq_set_type = vlynq_irq_type,
  193. };
  194. static struct irq_chip vlynq_local_chip = {
  195. .name = "vlynq local error",
  196. .irq_unmask = vlynq_irq_unmask,
  197. .irq_mask = vlynq_irq_mask,
  198. .irq_ack = vlynq_local_ack,
  199. };
  200. static struct irq_chip vlynq_remote_chip = {
  201. .name = "vlynq local error",
  202. .irq_unmask = vlynq_irq_unmask,
  203. .irq_mask = vlynq_irq_mask,
  204. .irq_ack = vlynq_remote_ack,
  205. };
  206. static int vlynq_setup_irq(struct vlynq_device *dev)
  207. {
  208. u32 val;
  209. int i, virq;
  210. if (dev->local_irq == dev->remote_irq) {
  211. printk(KERN_ERR
  212. "%s: local vlynq irq should be different from remote\n",
  213. dev_name(&dev->dev));
  214. return -EINVAL;
  215. }
  216. /* Clear local and remote error bits */
  217. writel(readl(&dev->local->status), &dev->local->status);
  218. writel(readl(&dev->remote->status), &dev->remote->status);
  219. /* Now setup interrupts */
  220. val = VLYNQ_CTRL_INT_VECTOR(dev->local_irq);
  221. val |= VLYNQ_CTRL_INT_ENABLE | VLYNQ_CTRL_INT_LOCAL |
  222. VLYNQ_CTRL_INT2CFG;
  223. val |= readl(&dev->local->control);
  224. writel(VLYNQ_INT_OFFSET, &dev->local->int_ptr);
  225. writel(val, &dev->local->control);
  226. val = VLYNQ_CTRL_INT_VECTOR(dev->remote_irq);
  227. val |= VLYNQ_CTRL_INT_ENABLE;
  228. val |= readl(&dev->remote->control);
  229. writel(VLYNQ_INT_OFFSET, &dev->remote->int_ptr);
  230. writel(val, &dev->remote->int_ptr);
  231. writel(val, &dev->remote->control);
  232. for (i = dev->irq_start; i <= dev->irq_end; i++) {
  233. virq = i - dev->irq_start;
  234. if (virq == dev->local_irq) {
  235. irq_set_chip_and_handler(i, &vlynq_local_chip,
  236. handle_level_irq);
  237. irq_set_chip_data(i, dev);
  238. } else if (virq == dev->remote_irq) {
  239. irq_set_chip_and_handler(i, &vlynq_remote_chip,
  240. handle_level_irq);
  241. irq_set_chip_data(i, dev);
  242. } else {
  243. irq_set_chip_and_handler(i, &vlynq_irq_chip,
  244. handle_simple_irq);
  245. irq_set_chip_data(i, dev);
  246. writel(0, &dev->remote->int_device[virq >> 2]);
  247. }
  248. }
  249. if (request_irq(dev->irq, vlynq_irq, IRQF_SHARED, "vlynq", dev)) {
  250. printk(KERN_ERR "%s: request_irq failed\n",
  251. dev_name(&dev->dev));
  252. return -EAGAIN;
  253. }
  254. return 0;
  255. }
  256. static void vlynq_device_release(struct device *dev)
  257. {
  258. struct vlynq_device *vdev = to_vlynq_device(dev);
  259. kfree(vdev);
  260. }
  261. static int vlynq_device_match(struct device *dev,
  262. struct device_driver *drv)
  263. {
  264. struct vlynq_device *vdev = to_vlynq_device(dev);
  265. struct vlynq_driver *vdrv = to_vlynq_driver(drv);
  266. struct vlynq_device_id *ids = vdrv->id_table;
  267. while (ids->id) {
  268. if (ids->id == vdev->dev_id) {
  269. vdev->divisor = ids->divisor;
  270. vlynq_set_drvdata(vdev, ids);
  271. printk(KERN_INFO "Driver found for VLYNQ "
  272. "device: %08x\n", vdev->dev_id);
  273. return 1;
  274. }
  275. printk(KERN_DEBUG "Not using the %08x VLYNQ device's driver"
  276. " for VLYNQ device: %08x\n", ids->id, vdev->dev_id);
  277. ids++;
  278. }
  279. return 0;
  280. }
  281. static int vlynq_device_probe(struct device *dev)
  282. {
  283. struct vlynq_device *vdev = to_vlynq_device(dev);
  284. struct vlynq_driver *drv = to_vlynq_driver(dev->driver);
  285. struct vlynq_device_id *id = vlynq_get_drvdata(vdev);
  286. int result = -ENODEV;
  287. if (drv->probe)
  288. result = drv->probe(vdev, id);
  289. if (result)
  290. put_device(dev);
  291. return result;
  292. }
  293. static void vlynq_device_remove(struct device *dev)
  294. {
  295. struct vlynq_driver *drv = to_vlynq_driver(dev->driver);
  296. if (drv->remove)
  297. drv->remove(to_vlynq_device(dev));
  298. }
  299. int __vlynq_register_driver(struct vlynq_driver *driver, struct module *owner)
  300. {
  301. driver->driver.name = driver->name;
  302. driver->driver.bus = &vlynq_bus_type;
  303. return driver_register(&driver->driver);
  304. }
  305. EXPORT_SYMBOL(__vlynq_register_driver);
  306. void vlynq_unregister_driver(struct vlynq_driver *driver)
  307. {
  308. driver_unregister(&driver->driver);
  309. }
  310. EXPORT_SYMBOL(vlynq_unregister_driver);
  311. /*
  312. * A VLYNQ remote device can clock the VLYNQ bus master
  313. * using a dedicated clock line. In that case, both the
  314. * remove device and the bus master should have the same
  315. * serial clock dividers configured. Iterate through the
  316. * 8 possible dividers until we actually link with the
  317. * device.
  318. */
  319. static int __vlynq_try_remote(struct vlynq_device *dev)
  320. {
  321. int i;
  322. vlynq_reset(dev);
  323. for (i = dev->dev_id ? vlynq_rdiv2 : vlynq_rdiv8; dev->dev_id ?
  324. i <= vlynq_rdiv8 : i >= vlynq_rdiv2;
  325. dev->dev_id ? i++ : i--) {
  326. if (!vlynq_linked(dev))
  327. break;
  328. writel((readl(&dev->remote->control) &
  329. ~VLYNQ_CTRL_CLOCK_MASK) |
  330. VLYNQ_CTRL_CLOCK_INT |
  331. VLYNQ_CTRL_CLOCK_DIV(i - vlynq_rdiv1),
  332. &dev->remote->control);
  333. writel((readl(&dev->local->control)
  334. & ~(VLYNQ_CTRL_CLOCK_INT |
  335. VLYNQ_CTRL_CLOCK_MASK)) |
  336. VLYNQ_CTRL_CLOCK_DIV(i - vlynq_rdiv1),
  337. &dev->local->control);
  338. if (vlynq_linked(dev)) {
  339. printk(KERN_DEBUG
  340. "%s: using remote clock divisor %d\n",
  341. dev_name(&dev->dev), i - vlynq_rdiv1 + 1);
  342. dev->divisor = i;
  343. return 0;
  344. } else {
  345. vlynq_reset(dev);
  346. }
  347. }
  348. return -ENODEV;
  349. }
  350. /*
  351. * A VLYNQ remote device can be clocked by the VLYNQ bus
  352. * master using a dedicated clock line. In that case, only
  353. * the bus master configures the serial clock divider.
  354. * Iterate through the 8 possible dividers until we
  355. * actually get a link with the device.
  356. */
  357. static int __vlynq_try_local(struct vlynq_device *dev)
  358. {
  359. int i;
  360. vlynq_reset(dev);
  361. for (i = dev->dev_id ? vlynq_ldiv2 : vlynq_ldiv8; dev->dev_id ?
  362. i <= vlynq_ldiv8 : i >= vlynq_ldiv2;
  363. dev->dev_id ? i++ : i--) {
  364. writel((readl(&dev->local->control) &
  365. ~VLYNQ_CTRL_CLOCK_MASK) |
  366. VLYNQ_CTRL_CLOCK_INT |
  367. VLYNQ_CTRL_CLOCK_DIV(i - vlynq_ldiv1),
  368. &dev->local->control);
  369. if (vlynq_linked(dev)) {
  370. printk(KERN_DEBUG
  371. "%s: using local clock divisor %d\n",
  372. dev_name(&dev->dev), i - vlynq_ldiv1 + 1);
  373. dev->divisor = i;
  374. return 0;
  375. } else {
  376. vlynq_reset(dev);
  377. }
  378. }
  379. return -ENODEV;
  380. }
  381. /*
  382. * When using external clocking method, serial clock
  383. * is supplied by an external oscillator, therefore we
  384. * should mask the local clock bit in the clock control
  385. * register for both the bus master and the remote device.
  386. */
  387. static int __vlynq_try_external(struct vlynq_device *dev)
  388. {
  389. vlynq_reset(dev);
  390. if (!vlynq_linked(dev))
  391. return -ENODEV;
  392. writel((readl(&dev->remote->control) &
  393. ~VLYNQ_CTRL_CLOCK_INT),
  394. &dev->remote->control);
  395. writel((readl(&dev->local->control) &
  396. ~VLYNQ_CTRL_CLOCK_INT),
  397. &dev->local->control);
  398. if (vlynq_linked(dev)) {
  399. printk(KERN_DEBUG "%s: using external clock\n",
  400. dev_name(&dev->dev));
  401. dev->divisor = vlynq_div_external;
  402. return 0;
  403. }
  404. return -ENODEV;
  405. }
  406. static int __vlynq_enable_device(struct vlynq_device *dev)
  407. {
  408. int result;
  409. struct plat_vlynq_ops *ops = dev->dev.platform_data;
  410. result = ops->on(dev);
  411. if (result)
  412. return result;
  413. switch (dev->divisor) {
  414. case vlynq_div_external:
  415. case vlynq_div_auto:
  416. /* When the device is brought from reset it should have clock
  417. * generation negotiated by hardware.
  418. * Check which device is generating clocks and perform setup
  419. * accordingly */
  420. if (vlynq_linked(dev) && readl(&dev->remote->control) &
  421. VLYNQ_CTRL_CLOCK_INT) {
  422. if (!__vlynq_try_remote(dev) ||
  423. !__vlynq_try_local(dev) ||
  424. !__vlynq_try_external(dev))
  425. return 0;
  426. } else {
  427. if (!__vlynq_try_external(dev) ||
  428. !__vlynq_try_local(dev) ||
  429. !__vlynq_try_remote(dev))
  430. return 0;
  431. }
  432. break;
  433. case vlynq_ldiv1:
  434. case vlynq_ldiv2:
  435. case vlynq_ldiv3:
  436. case vlynq_ldiv4:
  437. case vlynq_ldiv5:
  438. case vlynq_ldiv6:
  439. case vlynq_ldiv7:
  440. case vlynq_ldiv8:
  441. writel(VLYNQ_CTRL_CLOCK_INT |
  442. VLYNQ_CTRL_CLOCK_DIV(dev->divisor -
  443. vlynq_ldiv1), &dev->local->control);
  444. writel(0, &dev->remote->control);
  445. if (vlynq_linked(dev)) {
  446. printk(KERN_DEBUG
  447. "%s: using local clock divisor %d\n",
  448. dev_name(&dev->dev),
  449. dev->divisor - vlynq_ldiv1 + 1);
  450. return 0;
  451. }
  452. break;
  453. case vlynq_rdiv1:
  454. case vlynq_rdiv2:
  455. case vlynq_rdiv3:
  456. case vlynq_rdiv4:
  457. case vlynq_rdiv5:
  458. case vlynq_rdiv6:
  459. case vlynq_rdiv7:
  460. case vlynq_rdiv8:
  461. writel(0, &dev->local->control);
  462. writel(VLYNQ_CTRL_CLOCK_INT |
  463. VLYNQ_CTRL_CLOCK_DIV(dev->divisor -
  464. vlynq_rdiv1), &dev->remote->control);
  465. if (vlynq_linked(dev)) {
  466. printk(KERN_DEBUG
  467. "%s: using remote clock divisor %d\n",
  468. dev_name(&dev->dev),
  469. dev->divisor - vlynq_rdiv1 + 1);
  470. return 0;
  471. }
  472. break;
  473. }
  474. ops->off(dev);
  475. return -ENODEV;
  476. }
  477. int vlynq_enable_device(struct vlynq_device *dev)
  478. {
  479. struct plat_vlynq_ops *ops = dev->dev.platform_data;
  480. int result = -ENODEV;
  481. result = __vlynq_enable_device(dev);
  482. if (result)
  483. return result;
  484. result = vlynq_setup_irq(dev);
  485. if (result)
  486. ops->off(dev);
  487. dev->enabled = !result;
  488. return result;
  489. }
  490. EXPORT_SYMBOL(vlynq_enable_device);
  491. void vlynq_disable_device(struct vlynq_device *dev)
  492. {
  493. struct plat_vlynq_ops *ops = dev->dev.platform_data;
  494. dev->enabled = 0;
  495. free_irq(dev->irq, dev);
  496. ops->off(dev);
  497. }
  498. EXPORT_SYMBOL(vlynq_disable_device);
  499. int vlynq_set_local_mapping(struct vlynq_device *dev, u32 tx_offset,
  500. struct vlynq_mapping *mapping)
  501. {
  502. int i;
  503. if (!dev->enabled)
  504. return -ENXIO;
  505. writel(tx_offset, &dev->local->tx_offset);
  506. for (i = 0; i < 4; i++) {
  507. writel(mapping[i].offset, &dev->local->rx_mapping[i].offset);
  508. writel(mapping[i].size, &dev->local->rx_mapping[i].size);
  509. }
  510. return 0;
  511. }
  512. EXPORT_SYMBOL(vlynq_set_local_mapping);
  513. int vlynq_set_remote_mapping(struct vlynq_device *dev, u32 tx_offset,
  514. struct vlynq_mapping *mapping)
  515. {
  516. int i;
  517. if (!dev->enabled)
  518. return -ENXIO;
  519. writel(tx_offset, &dev->remote->tx_offset);
  520. for (i = 0; i < 4; i++) {
  521. writel(mapping[i].offset, &dev->remote->rx_mapping[i].offset);
  522. writel(mapping[i].size, &dev->remote->rx_mapping[i].size);
  523. }
  524. return 0;
  525. }
  526. EXPORT_SYMBOL(vlynq_set_remote_mapping);
  527. int vlynq_set_local_irq(struct vlynq_device *dev, int virq)
  528. {
  529. int irq = dev->irq_start + virq;
  530. if (dev->enabled)
  531. return -EBUSY;
  532. if ((irq < dev->irq_start) || (irq > dev->irq_end))
  533. return -EINVAL;
  534. if (virq == dev->remote_irq)
  535. return -EINVAL;
  536. dev->local_irq = virq;
  537. return 0;
  538. }
  539. EXPORT_SYMBOL(vlynq_set_local_irq);
  540. int vlynq_set_remote_irq(struct vlynq_device *dev, int virq)
  541. {
  542. int irq = dev->irq_start + virq;
  543. if (dev->enabled)
  544. return -EBUSY;
  545. if ((irq < dev->irq_start) || (irq > dev->irq_end))
  546. return -EINVAL;
  547. if (virq == dev->local_irq)
  548. return -EINVAL;
  549. dev->remote_irq = virq;
  550. return 0;
  551. }
  552. EXPORT_SYMBOL(vlynq_set_remote_irq);
  553. static int vlynq_probe(struct platform_device *pdev)
  554. {
  555. struct vlynq_device *dev;
  556. struct resource *regs_res, *mem_res, *irq_res;
  557. int len, result;
  558. regs_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
  559. if (!regs_res)
  560. return -ENODEV;
  561. mem_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mem");
  562. if (!mem_res)
  563. return -ENODEV;
  564. irq_res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "devirq");
  565. if (!irq_res)
  566. return -ENODEV;
  567. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  568. if (!dev) {
  569. printk(KERN_ERR
  570. "vlynq: failed to allocate device structure\n");
  571. return -ENOMEM;
  572. }
  573. dev->id = pdev->id;
  574. dev->dev.bus = &vlynq_bus_type;
  575. dev->dev.parent = &pdev->dev;
  576. dev_set_name(&dev->dev, "vlynq%d", dev->id);
  577. dev->dev.platform_data = pdev->dev.platform_data;
  578. dev->dev.release = vlynq_device_release;
  579. dev->regs_start = regs_res->start;
  580. dev->regs_end = regs_res->end;
  581. dev->mem_start = mem_res->start;
  582. dev->mem_end = mem_res->end;
  583. len = resource_size(regs_res);
  584. if (!request_mem_region(regs_res->start, len, dev_name(&dev->dev))) {
  585. printk(KERN_ERR "%s: Can't request vlynq registers\n",
  586. dev_name(&dev->dev));
  587. result = -ENXIO;
  588. goto fail_request;
  589. }
  590. dev->local = ioremap(regs_res->start, len);
  591. if (!dev->local) {
  592. printk(KERN_ERR "%s: Can't remap vlynq registers\n",
  593. dev_name(&dev->dev));
  594. result = -ENXIO;
  595. goto fail_remap;
  596. }
  597. dev->remote = (struct vlynq_regs *)((void *)dev->local +
  598. VLYNQ_REMOTE_OFFSET);
  599. dev->irq = platform_get_irq_byname(pdev, "irq");
  600. dev->irq_start = irq_res->start;
  601. dev->irq_end = irq_res->end;
  602. dev->local_irq = dev->irq_end - dev->irq_start;
  603. dev->remote_irq = dev->local_irq - 1;
  604. if (device_register(&dev->dev))
  605. goto fail_register;
  606. platform_set_drvdata(pdev, dev);
  607. printk(KERN_INFO "%s: regs 0x%p, irq %d, mem 0x%p\n",
  608. dev_name(&dev->dev), (void *)dev->regs_start, dev->irq,
  609. (void *)dev->mem_start);
  610. dev->dev_id = 0;
  611. dev->divisor = vlynq_div_auto;
  612. result = __vlynq_enable_device(dev);
  613. if (result == 0) {
  614. dev->dev_id = readl(&dev->remote->chip);
  615. ((struct plat_vlynq_ops *)(dev->dev.platform_data))->off(dev);
  616. }
  617. if (dev->dev_id)
  618. printk(KERN_INFO "Found a VLYNQ device: %08x\n", dev->dev_id);
  619. return 0;
  620. fail_register:
  621. iounmap(dev->local);
  622. fail_remap:
  623. fail_request:
  624. release_mem_region(regs_res->start, len);
  625. kfree(dev);
  626. return result;
  627. }
  628. static int vlynq_remove(struct platform_device *pdev)
  629. {
  630. struct vlynq_device *dev = platform_get_drvdata(pdev);
  631. device_unregister(&dev->dev);
  632. iounmap(dev->local);
  633. release_mem_region(dev->regs_start,
  634. dev->regs_end - dev->regs_start + 1);
  635. kfree(dev);
  636. return 0;
  637. }
  638. static struct platform_driver vlynq_platform_driver = {
  639. .driver.name = "vlynq",
  640. .probe = vlynq_probe,
  641. .remove = vlynq_remove,
  642. };
  643. struct bus_type vlynq_bus_type = {
  644. .name = "vlynq",
  645. .match = vlynq_device_match,
  646. .probe = vlynq_device_probe,
  647. .remove = vlynq_device_remove,
  648. };
  649. EXPORT_SYMBOL(vlynq_bus_type);
  650. static int vlynq_init(void)
  651. {
  652. int res = 0;
  653. res = bus_register(&vlynq_bus_type);
  654. if (res)
  655. goto fail_bus;
  656. res = platform_driver_register(&vlynq_platform_driver);
  657. if (res)
  658. goto fail_platform;
  659. return 0;
  660. fail_platform:
  661. bus_unregister(&vlynq_bus_type);
  662. fail_bus:
  663. return res;
  664. }
  665. static void vlynq_exit(void)
  666. {
  667. platform_driver_unregister(&vlynq_platform_driver);
  668. bus_unregister(&vlynq_bus_type);
  669. }
  670. module_init(vlynq_init);
  671. module_exit(vlynq_exit);