vlynq.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  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 int 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. return 0;
  299. }
  300. int __vlynq_register_driver(struct vlynq_driver *driver, struct module *owner)
  301. {
  302. driver->driver.name = driver->name;
  303. driver->driver.bus = &vlynq_bus_type;
  304. return driver_register(&driver->driver);
  305. }
  306. EXPORT_SYMBOL(__vlynq_register_driver);
  307. void vlynq_unregister_driver(struct vlynq_driver *driver)
  308. {
  309. driver_unregister(&driver->driver);
  310. }
  311. EXPORT_SYMBOL(vlynq_unregister_driver);
  312. /*
  313. * A VLYNQ remote device can clock the VLYNQ bus master
  314. * using a dedicated clock line. In that case, both the
  315. * remove device and the bus master should have the same
  316. * serial clock dividers configured. Iterate through the
  317. * 8 possible dividers until we actually link with the
  318. * device.
  319. */
  320. static int __vlynq_try_remote(struct vlynq_device *dev)
  321. {
  322. int i;
  323. vlynq_reset(dev);
  324. for (i = dev->dev_id ? vlynq_rdiv2 : vlynq_rdiv8; dev->dev_id ?
  325. i <= vlynq_rdiv8 : i >= vlynq_rdiv2;
  326. dev->dev_id ? i++ : i--) {
  327. if (!vlynq_linked(dev))
  328. break;
  329. writel((readl(&dev->remote->control) &
  330. ~VLYNQ_CTRL_CLOCK_MASK) |
  331. VLYNQ_CTRL_CLOCK_INT |
  332. VLYNQ_CTRL_CLOCK_DIV(i - vlynq_rdiv1),
  333. &dev->remote->control);
  334. writel((readl(&dev->local->control)
  335. & ~(VLYNQ_CTRL_CLOCK_INT |
  336. VLYNQ_CTRL_CLOCK_MASK)) |
  337. VLYNQ_CTRL_CLOCK_DIV(i - vlynq_rdiv1),
  338. &dev->local->control);
  339. if (vlynq_linked(dev)) {
  340. printk(KERN_DEBUG
  341. "%s: using remote clock divisor %d\n",
  342. dev_name(&dev->dev), i - vlynq_rdiv1 + 1);
  343. dev->divisor = i;
  344. return 0;
  345. } else {
  346. vlynq_reset(dev);
  347. }
  348. }
  349. return -ENODEV;
  350. }
  351. /*
  352. * A VLYNQ remote device can be clocked by the VLYNQ bus
  353. * master using a dedicated clock line. In that case, only
  354. * the bus master configures the serial clock divider.
  355. * Iterate through the 8 possible dividers until we
  356. * actually get a link with the device.
  357. */
  358. static int __vlynq_try_local(struct vlynq_device *dev)
  359. {
  360. int i;
  361. vlynq_reset(dev);
  362. for (i = dev->dev_id ? vlynq_ldiv2 : vlynq_ldiv8; dev->dev_id ?
  363. i <= vlynq_ldiv8 : i >= vlynq_ldiv2;
  364. dev->dev_id ? i++ : i--) {
  365. writel((readl(&dev->local->control) &
  366. ~VLYNQ_CTRL_CLOCK_MASK) |
  367. VLYNQ_CTRL_CLOCK_INT |
  368. VLYNQ_CTRL_CLOCK_DIV(i - vlynq_ldiv1),
  369. &dev->local->control);
  370. if (vlynq_linked(dev)) {
  371. printk(KERN_DEBUG
  372. "%s: using local clock divisor %d\n",
  373. dev_name(&dev->dev), i - vlynq_ldiv1 + 1);
  374. dev->divisor = i;
  375. return 0;
  376. } else {
  377. vlynq_reset(dev);
  378. }
  379. }
  380. return -ENODEV;
  381. }
  382. /*
  383. * When using external clocking method, serial clock
  384. * is supplied by an external oscillator, therefore we
  385. * should mask the local clock bit in the clock control
  386. * register for both the bus master and the remote device.
  387. */
  388. static int __vlynq_try_external(struct vlynq_device *dev)
  389. {
  390. vlynq_reset(dev);
  391. if (!vlynq_linked(dev))
  392. return -ENODEV;
  393. writel((readl(&dev->remote->control) &
  394. ~VLYNQ_CTRL_CLOCK_INT),
  395. &dev->remote->control);
  396. writel((readl(&dev->local->control) &
  397. ~VLYNQ_CTRL_CLOCK_INT),
  398. &dev->local->control);
  399. if (vlynq_linked(dev)) {
  400. printk(KERN_DEBUG "%s: using external clock\n",
  401. dev_name(&dev->dev));
  402. dev->divisor = vlynq_div_external;
  403. return 0;
  404. }
  405. return -ENODEV;
  406. }
  407. static int __vlynq_enable_device(struct vlynq_device *dev)
  408. {
  409. int result;
  410. struct plat_vlynq_ops *ops = dev->dev.platform_data;
  411. result = ops->on(dev);
  412. if (result)
  413. return result;
  414. switch (dev->divisor) {
  415. case vlynq_div_external:
  416. case vlynq_div_auto:
  417. /* When the device is brought from reset it should have clock
  418. * generation negotiated by hardware.
  419. * Check which device is generating clocks and perform setup
  420. * accordingly */
  421. if (vlynq_linked(dev) && readl(&dev->remote->control) &
  422. VLYNQ_CTRL_CLOCK_INT) {
  423. if (!__vlynq_try_remote(dev) ||
  424. !__vlynq_try_local(dev) ||
  425. !__vlynq_try_external(dev))
  426. return 0;
  427. } else {
  428. if (!__vlynq_try_external(dev) ||
  429. !__vlynq_try_local(dev) ||
  430. !__vlynq_try_remote(dev))
  431. return 0;
  432. }
  433. break;
  434. case vlynq_ldiv1:
  435. case vlynq_ldiv2:
  436. case vlynq_ldiv3:
  437. case vlynq_ldiv4:
  438. case vlynq_ldiv5:
  439. case vlynq_ldiv6:
  440. case vlynq_ldiv7:
  441. case vlynq_ldiv8:
  442. writel(VLYNQ_CTRL_CLOCK_INT |
  443. VLYNQ_CTRL_CLOCK_DIV(dev->divisor -
  444. vlynq_ldiv1), &dev->local->control);
  445. writel(0, &dev->remote->control);
  446. if (vlynq_linked(dev)) {
  447. printk(KERN_DEBUG
  448. "%s: using local clock divisor %d\n",
  449. dev_name(&dev->dev),
  450. dev->divisor - vlynq_ldiv1 + 1);
  451. return 0;
  452. }
  453. break;
  454. case vlynq_rdiv1:
  455. case vlynq_rdiv2:
  456. case vlynq_rdiv3:
  457. case vlynq_rdiv4:
  458. case vlynq_rdiv5:
  459. case vlynq_rdiv6:
  460. case vlynq_rdiv7:
  461. case vlynq_rdiv8:
  462. writel(0, &dev->local->control);
  463. writel(VLYNQ_CTRL_CLOCK_INT |
  464. VLYNQ_CTRL_CLOCK_DIV(dev->divisor -
  465. vlynq_rdiv1), &dev->remote->control);
  466. if (vlynq_linked(dev)) {
  467. printk(KERN_DEBUG
  468. "%s: using remote clock divisor %d\n",
  469. dev_name(&dev->dev),
  470. dev->divisor - vlynq_rdiv1 + 1);
  471. return 0;
  472. }
  473. break;
  474. }
  475. ops->off(dev);
  476. return -ENODEV;
  477. }
  478. int vlynq_enable_device(struct vlynq_device *dev)
  479. {
  480. struct plat_vlynq_ops *ops = dev->dev.platform_data;
  481. int result = -ENODEV;
  482. result = __vlynq_enable_device(dev);
  483. if (result)
  484. return result;
  485. result = vlynq_setup_irq(dev);
  486. if (result)
  487. ops->off(dev);
  488. dev->enabled = !result;
  489. return result;
  490. }
  491. EXPORT_SYMBOL(vlynq_enable_device);
  492. void vlynq_disable_device(struct vlynq_device *dev)
  493. {
  494. struct plat_vlynq_ops *ops = dev->dev.platform_data;
  495. dev->enabled = 0;
  496. free_irq(dev->irq, dev);
  497. ops->off(dev);
  498. }
  499. EXPORT_SYMBOL(vlynq_disable_device);
  500. int vlynq_set_local_mapping(struct vlynq_device *dev, u32 tx_offset,
  501. struct vlynq_mapping *mapping)
  502. {
  503. int i;
  504. if (!dev->enabled)
  505. return -ENXIO;
  506. writel(tx_offset, &dev->local->tx_offset);
  507. for (i = 0; i < 4; i++) {
  508. writel(mapping[i].offset, &dev->local->rx_mapping[i].offset);
  509. writel(mapping[i].size, &dev->local->rx_mapping[i].size);
  510. }
  511. return 0;
  512. }
  513. EXPORT_SYMBOL(vlynq_set_local_mapping);
  514. int vlynq_set_remote_mapping(struct vlynq_device *dev, u32 tx_offset,
  515. struct vlynq_mapping *mapping)
  516. {
  517. int i;
  518. if (!dev->enabled)
  519. return -ENXIO;
  520. writel(tx_offset, &dev->remote->tx_offset);
  521. for (i = 0; i < 4; i++) {
  522. writel(mapping[i].offset, &dev->remote->rx_mapping[i].offset);
  523. writel(mapping[i].size, &dev->remote->rx_mapping[i].size);
  524. }
  525. return 0;
  526. }
  527. EXPORT_SYMBOL(vlynq_set_remote_mapping);
  528. int vlynq_set_local_irq(struct vlynq_device *dev, int virq)
  529. {
  530. int irq = dev->irq_start + virq;
  531. if (dev->enabled)
  532. return -EBUSY;
  533. if ((irq < dev->irq_start) || (irq > dev->irq_end))
  534. return -EINVAL;
  535. if (virq == dev->remote_irq)
  536. return -EINVAL;
  537. dev->local_irq = virq;
  538. return 0;
  539. }
  540. EXPORT_SYMBOL(vlynq_set_local_irq);
  541. int vlynq_set_remote_irq(struct vlynq_device *dev, int virq)
  542. {
  543. int irq = dev->irq_start + virq;
  544. if (dev->enabled)
  545. return -EBUSY;
  546. if ((irq < dev->irq_start) || (irq > dev->irq_end))
  547. return -EINVAL;
  548. if (virq == dev->local_irq)
  549. return -EINVAL;
  550. dev->remote_irq = virq;
  551. return 0;
  552. }
  553. EXPORT_SYMBOL(vlynq_set_remote_irq);
  554. static int vlynq_probe(struct platform_device *pdev)
  555. {
  556. struct vlynq_device *dev;
  557. struct resource *regs_res, *mem_res, *irq_res;
  558. int len, result;
  559. regs_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
  560. if (!regs_res)
  561. return -ENODEV;
  562. mem_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mem");
  563. if (!mem_res)
  564. return -ENODEV;
  565. irq_res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "devirq");
  566. if (!irq_res)
  567. return -ENODEV;
  568. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  569. if (!dev) {
  570. printk(KERN_ERR
  571. "vlynq: failed to allocate device structure\n");
  572. return -ENOMEM;
  573. }
  574. dev->id = pdev->id;
  575. dev->dev.bus = &vlynq_bus_type;
  576. dev->dev.parent = &pdev->dev;
  577. dev_set_name(&dev->dev, "vlynq%d", dev->id);
  578. dev->dev.platform_data = pdev->dev.platform_data;
  579. dev->dev.release = vlynq_device_release;
  580. dev->regs_start = regs_res->start;
  581. dev->regs_end = regs_res->end;
  582. dev->mem_start = mem_res->start;
  583. dev->mem_end = mem_res->end;
  584. len = resource_size(regs_res);
  585. if (!request_mem_region(regs_res->start, len, dev_name(&dev->dev))) {
  586. printk(KERN_ERR "%s: Can't request vlynq registers\n",
  587. dev_name(&dev->dev));
  588. result = -ENXIO;
  589. goto fail_request;
  590. }
  591. dev->local = ioremap(regs_res->start, len);
  592. if (!dev->local) {
  593. printk(KERN_ERR "%s: Can't remap vlynq registers\n",
  594. dev_name(&dev->dev));
  595. result = -ENXIO;
  596. goto fail_remap;
  597. }
  598. dev->remote = (struct vlynq_regs *)((void *)dev->local +
  599. VLYNQ_REMOTE_OFFSET);
  600. dev->irq = platform_get_irq_byname(pdev, "irq");
  601. dev->irq_start = irq_res->start;
  602. dev->irq_end = irq_res->end;
  603. dev->local_irq = dev->irq_end - dev->irq_start;
  604. dev->remote_irq = dev->local_irq - 1;
  605. if (device_register(&dev->dev))
  606. goto fail_register;
  607. platform_set_drvdata(pdev, dev);
  608. printk(KERN_INFO "%s: regs 0x%p, irq %d, mem 0x%p\n",
  609. dev_name(&dev->dev), (void *)dev->regs_start, dev->irq,
  610. (void *)dev->mem_start);
  611. dev->dev_id = 0;
  612. dev->divisor = vlynq_div_auto;
  613. result = __vlynq_enable_device(dev);
  614. if (result == 0) {
  615. dev->dev_id = readl(&dev->remote->chip);
  616. ((struct plat_vlynq_ops *)(dev->dev.platform_data))->off(dev);
  617. }
  618. if (dev->dev_id)
  619. printk(KERN_INFO "Found a VLYNQ device: %08x\n", dev->dev_id);
  620. return 0;
  621. fail_register:
  622. iounmap(dev->local);
  623. fail_remap:
  624. fail_request:
  625. release_mem_region(regs_res->start, len);
  626. kfree(dev);
  627. return result;
  628. }
  629. static int vlynq_remove(struct platform_device *pdev)
  630. {
  631. struct vlynq_device *dev = platform_get_drvdata(pdev);
  632. device_unregister(&dev->dev);
  633. iounmap(dev->local);
  634. release_mem_region(dev->regs_start,
  635. dev->regs_end - dev->regs_start + 1);
  636. kfree(dev);
  637. return 0;
  638. }
  639. static struct platform_driver vlynq_platform_driver = {
  640. .driver.name = "vlynq",
  641. .probe = vlynq_probe,
  642. .remove = vlynq_remove,
  643. };
  644. struct bus_type vlynq_bus_type = {
  645. .name = "vlynq",
  646. .match = vlynq_device_match,
  647. .probe = vlynq_device_probe,
  648. .remove = vlynq_device_remove,
  649. };
  650. EXPORT_SYMBOL(vlynq_bus_type);
  651. static int vlynq_init(void)
  652. {
  653. int res = 0;
  654. res = bus_register(&vlynq_bus_type);
  655. if (res)
  656. goto fail_bus;
  657. res = platform_driver_register(&vlynq_platform_driver);
  658. if (res)
  659. goto fail_platform;
  660. return 0;
  661. fail_platform:
  662. bus_unregister(&vlynq_bus_type);
  663. fail_bus:
  664. return res;
  665. }
  666. static void vlynq_exit(void)
  667. {
  668. platform_driver_unregister(&vlynq_platform_driver);
  669. bus_unregister(&vlynq_bus_type);
  670. }
  671. module_init(vlynq_init);
  672. module_exit(vlynq_exit);