tscan1.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * tscan1.c: driver for Technologic Systems TS-CAN1 PC104 boards
  3. *
  4. * Copyright 2010 Andre B. Oliveira
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /*
  20. * References:
  21. * - Getting started with TS-CAN1, Technologic Systems, Jun 2009
  22. * http://www.embeddedarm.com/documentation/ts-can1-manual.pdf
  23. */
  24. #include <linux/init.h>
  25. #include <linux/io.h>
  26. #include <linux/ioport.h>
  27. #include <linux/isa.h>
  28. #include <linux/module.h>
  29. #include <linux/netdevice.h>
  30. #include "sja1000.h"
  31. MODULE_DESCRIPTION("Driver for Technologic Systems TS-CAN1 PC104 boards");
  32. MODULE_AUTHOR("Andre B. Oliveira <anbadeol@gmail.com>");
  33. MODULE_LICENSE("GPL");
  34. /* Maximum number of boards (one in each JP1:JP2 setting of IO address) */
  35. #define TSCAN1_MAXDEV 4
  36. /* PLD registers address offsets */
  37. #define TSCAN1_ID1 0
  38. #define TSCAN1_ID2 1
  39. #define TSCAN1_VERSION 2
  40. #define TSCAN1_LED 3
  41. #define TSCAN1_PAGE 4
  42. #define TSCAN1_MODE 5
  43. #define TSCAN1_JUMPERS 6
  44. /* PLD board identifier registers magic values */
  45. #define TSCAN1_ID1_VALUE 0xf6
  46. #define TSCAN1_ID2_VALUE 0xb9
  47. /* PLD mode register SJA1000 IO enable bit */
  48. #define TSCAN1_MODE_ENABLE 0x40
  49. /* PLD jumpers register bits */
  50. #define TSCAN1_JP4 0x10
  51. #define TSCAN1_JP5 0x20
  52. /* PLD IO base addresses start */
  53. #define TSCAN1_PLD_ADDRESS 0x150
  54. /* PLD register space size */
  55. #define TSCAN1_PLD_SIZE 8
  56. /* SJA1000 register space size */
  57. #define TSCAN1_SJA1000_SIZE 32
  58. /* SJA1000 crystal frequency (16MHz) */
  59. #define TSCAN1_SJA1000_XTAL 16000000
  60. /* SJA1000 IO base addresses */
  61. static const unsigned short tscan1_sja1000_addresses[] = {
  62. 0x100, 0x120, 0x180, 0x1a0, 0x200, 0x240, 0x280, 0x320
  63. };
  64. /* Read SJA1000 register */
  65. static u8 tscan1_read(const struct sja1000_priv *priv, int reg)
  66. {
  67. return inb((unsigned long)priv->reg_base + reg);
  68. }
  69. /* Write SJA1000 register */
  70. static void tscan1_write(const struct sja1000_priv *priv, int reg, u8 val)
  71. {
  72. outb(val, (unsigned long)priv->reg_base + reg);
  73. }
  74. /* Probe for a TS-CAN1 board with JP2:JP1 jumper setting ID */
  75. static int tscan1_probe(struct device *dev, unsigned id)
  76. {
  77. struct net_device *netdev;
  78. struct sja1000_priv *priv;
  79. unsigned long pld_base, sja1000_base;
  80. int irq, i;
  81. pld_base = TSCAN1_PLD_ADDRESS + id * TSCAN1_PLD_SIZE;
  82. if (!request_region(pld_base, TSCAN1_PLD_SIZE, dev_name(dev)))
  83. return -EBUSY;
  84. if (inb(pld_base + TSCAN1_ID1) != TSCAN1_ID1_VALUE ||
  85. inb(pld_base + TSCAN1_ID2) != TSCAN1_ID2_VALUE) {
  86. release_region(pld_base, TSCAN1_PLD_SIZE);
  87. return -ENODEV;
  88. }
  89. switch (inb(pld_base + TSCAN1_JUMPERS) & (TSCAN1_JP4 | TSCAN1_JP5)) {
  90. case TSCAN1_JP4:
  91. irq = 6;
  92. break;
  93. case TSCAN1_JP5:
  94. irq = 7;
  95. break;
  96. case TSCAN1_JP4 | TSCAN1_JP5:
  97. irq = 5;
  98. break;
  99. default:
  100. dev_err(dev, "invalid JP4:JP5 setting (no IRQ)\n");
  101. release_region(pld_base, TSCAN1_PLD_SIZE);
  102. return -EINVAL;
  103. }
  104. netdev = alloc_sja1000dev(0);
  105. if (!netdev) {
  106. release_region(pld_base, TSCAN1_PLD_SIZE);
  107. return -ENOMEM;
  108. }
  109. dev_set_drvdata(dev, netdev);
  110. SET_NETDEV_DEV(netdev, dev);
  111. netdev->base_addr = pld_base;
  112. netdev->irq = irq;
  113. priv = netdev_priv(netdev);
  114. priv->read_reg = tscan1_read;
  115. priv->write_reg = tscan1_write;
  116. priv->can.clock.freq = TSCAN1_SJA1000_XTAL / 2;
  117. priv->cdr = CDR_CBP | CDR_CLK_OFF;
  118. priv->ocr = OCR_TX0_PUSHPULL;
  119. /* Select the first SJA1000 IO address that is free and that works */
  120. for (i = 0; i < ARRAY_SIZE(tscan1_sja1000_addresses); i++) {
  121. sja1000_base = tscan1_sja1000_addresses[i];
  122. if (!request_region(sja1000_base, TSCAN1_SJA1000_SIZE,
  123. dev_name(dev)))
  124. continue;
  125. /* Set SJA1000 IO base address and enable it */
  126. outb(TSCAN1_MODE_ENABLE | i, pld_base + TSCAN1_MODE);
  127. priv->reg_base = (void __iomem *)sja1000_base;
  128. if (!register_sja1000dev(netdev)) {
  129. /* SJA1000 probe succeeded; turn LED off and return */
  130. outb(0, pld_base + TSCAN1_LED);
  131. netdev_info(netdev, "TS-CAN1 at 0x%lx 0x%lx irq %d\n",
  132. pld_base, sja1000_base, irq);
  133. return 0;
  134. }
  135. /* SJA1000 probe failed; release and try next address */
  136. outb(0, pld_base + TSCAN1_MODE);
  137. release_region(sja1000_base, TSCAN1_SJA1000_SIZE);
  138. }
  139. dev_err(dev, "failed to assign SJA1000 IO address\n");
  140. dev_set_drvdata(dev, NULL);
  141. free_sja1000dev(netdev);
  142. release_region(pld_base, TSCAN1_PLD_SIZE);
  143. return -ENXIO;
  144. }
  145. static int tscan1_remove(struct device *dev, unsigned id /*unused*/)
  146. {
  147. struct net_device *netdev;
  148. struct sja1000_priv *priv;
  149. unsigned long pld_base, sja1000_base;
  150. netdev = dev_get_drvdata(dev);
  151. unregister_sja1000dev(netdev);
  152. dev_set_drvdata(dev, NULL);
  153. priv = netdev_priv(netdev);
  154. pld_base = netdev->base_addr;
  155. sja1000_base = (unsigned long)priv->reg_base;
  156. outb(0, pld_base + TSCAN1_MODE); /* disable SJA1000 IO space */
  157. release_region(sja1000_base, TSCAN1_SJA1000_SIZE);
  158. release_region(pld_base, TSCAN1_PLD_SIZE);
  159. free_sja1000dev(netdev);
  160. return 0;
  161. }
  162. static struct isa_driver tscan1_isa_driver = {
  163. .probe = tscan1_probe,
  164. .remove = tscan1_remove,
  165. .driver = {
  166. .name = "tscan1",
  167. },
  168. };
  169. static int __init tscan1_init(void)
  170. {
  171. return isa_register_driver(&tscan1_isa_driver, TSCAN1_MAXDEV);
  172. }
  173. module_init(tscan1_init);
  174. static void __exit tscan1_exit(void)
  175. {
  176. isa_unregister_driver(&tscan1_isa_driver);
  177. }
  178. module_exit(tscan1_exit);