spi_ks8995.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. * SPI driver for Micrel/Kendin KS8995M and KSZ8864RMN ethernet switches
  3. *
  4. * Copyright (C) 2008 Gabor Juhos <juhosg at openwrt.org>
  5. *
  6. * This file was based on: drivers/spi/at25.c
  7. * Copyright (C) 2006 David Brownell
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License version 2 as published
  11. * by the Free Software Foundation.
  12. */
  13. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  14. #include <linux/types.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/delay.h>
  18. #include <linux/device.h>
  19. #include <linux/spi/spi.h>
  20. #define DRV_VERSION "0.1.1"
  21. #define DRV_DESC "Micrel KS8995 Ethernet switch SPI driver"
  22. /* ------------------------------------------------------------------------ */
  23. #define KS8995_REG_ID0 0x00 /* Chip ID0 */
  24. #define KS8995_REG_ID1 0x01 /* Chip ID1 */
  25. #define KS8995_REG_GC0 0x02 /* Global Control 0 */
  26. #define KS8995_REG_GC1 0x03 /* Global Control 1 */
  27. #define KS8995_REG_GC2 0x04 /* Global Control 2 */
  28. #define KS8995_REG_GC3 0x05 /* Global Control 3 */
  29. #define KS8995_REG_GC4 0x06 /* Global Control 4 */
  30. #define KS8995_REG_GC5 0x07 /* Global Control 5 */
  31. #define KS8995_REG_GC6 0x08 /* Global Control 6 */
  32. #define KS8995_REG_GC7 0x09 /* Global Control 7 */
  33. #define KS8995_REG_GC8 0x0a /* Global Control 8 */
  34. #define KS8995_REG_GC9 0x0b /* Global Control 9 */
  35. #define KS8995_REG_PC(p, r) ((0x10 * p) + r) /* Port Control */
  36. #define KS8995_REG_PS(p, r) ((0x10 * p) + r + 0xe) /* Port Status */
  37. #define KS8995_REG_TPC0 0x60 /* TOS Priority Control 0 */
  38. #define KS8995_REG_TPC1 0x61 /* TOS Priority Control 1 */
  39. #define KS8995_REG_TPC2 0x62 /* TOS Priority Control 2 */
  40. #define KS8995_REG_TPC3 0x63 /* TOS Priority Control 3 */
  41. #define KS8995_REG_TPC4 0x64 /* TOS Priority Control 4 */
  42. #define KS8995_REG_TPC5 0x65 /* TOS Priority Control 5 */
  43. #define KS8995_REG_TPC6 0x66 /* TOS Priority Control 6 */
  44. #define KS8995_REG_TPC7 0x67 /* TOS Priority Control 7 */
  45. #define KS8995_REG_MAC0 0x68 /* MAC address 0 */
  46. #define KS8995_REG_MAC1 0x69 /* MAC address 1 */
  47. #define KS8995_REG_MAC2 0x6a /* MAC address 2 */
  48. #define KS8995_REG_MAC3 0x6b /* MAC address 3 */
  49. #define KS8995_REG_MAC4 0x6c /* MAC address 4 */
  50. #define KS8995_REG_MAC5 0x6d /* MAC address 5 */
  51. #define KS8995_REG_IAC0 0x6e /* Indirect Access Control 0 */
  52. #define KS8995_REG_IAC1 0x6f /* Indirect Access Control 0 */
  53. #define KS8995_REG_IAD7 0x70 /* Indirect Access Data 7 */
  54. #define KS8995_REG_IAD6 0x71 /* Indirect Access Data 6 */
  55. #define KS8995_REG_IAD5 0x72 /* Indirect Access Data 5 */
  56. #define KS8995_REG_IAD4 0x73 /* Indirect Access Data 4 */
  57. #define KS8995_REG_IAD3 0x74 /* Indirect Access Data 3 */
  58. #define KS8995_REG_IAD2 0x75 /* Indirect Access Data 2 */
  59. #define KS8995_REG_IAD1 0x76 /* Indirect Access Data 1 */
  60. #define KS8995_REG_IAD0 0x77 /* Indirect Access Data 0 */
  61. #define KSZ8864_REG_ID1 0xfe /* Chip ID in bit 7 */
  62. #define KS8995_REGS_SIZE 0x80
  63. #define KSZ8864_REGS_SIZE 0x100
  64. #define ID1_CHIPID_M 0xf
  65. #define ID1_CHIPID_S 4
  66. #define ID1_REVISION_M 0x7
  67. #define ID1_REVISION_S 1
  68. #define ID1_START_SW 1 /* start the switch */
  69. #define FAMILY_KS8995 0x95
  70. #define CHIPID_M 0
  71. #define KS8995_CMD_WRITE 0x02U
  72. #define KS8995_CMD_READ 0x03U
  73. #define KS8995_RESET_DELAY 10 /* usec */
  74. struct ks8995_pdata {
  75. /* not yet implemented */
  76. };
  77. struct ks8995_switch {
  78. struct spi_device *spi;
  79. struct mutex lock;
  80. struct ks8995_pdata *pdata;
  81. struct bin_attribute regs_attr;
  82. };
  83. static inline u8 get_chip_id(u8 val)
  84. {
  85. return (val >> ID1_CHIPID_S) & ID1_CHIPID_M;
  86. }
  87. static inline u8 get_chip_rev(u8 val)
  88. {
  89. return (val >> ID1_REVISION_S) & ID1_REVISION_M;
  90. }
  91. /* ------------------------------------------------------------------------ */
  92. static int ks8995_read(struct ks8995_switch *ks, char *buf,
  93. unsigned offset, size_t count)
  94. {
  95. u8 cmd[2];
  96. struct spi_transfer t[2];
  97. struct spi_message m;
  98. int err;
  99. spi_message_init(&m);
  100. memset(&t, 0, sizeof(t));
  101. t[0].tx_buf = cmd;
  102. t[0].len = sizeof(cmd);
  103. spi_message_add_tail(&t[0], &m);
  104. t[1].rx_buf = buf;
  105. t[1].len = count;
  106. spi_message_add_tail(&t[1], &m);
  107. cmd[0] = KS8995_CMD_READ;
  108. cmd[1] = offset;
  109. mutex_lock(&ks->lock);
  110. err = spi_sync(ks->spi, &m);
  111. mutex_unlock(&ks->lock);
  112. return err ? err : count;
  113. }
  114. static int ks8995_write(struct ks8995_switch *ks, char *buf,
  115. unsigned offset, size_t count)
  116. {
  117. u8 cmd[2];
  118. struct spi_transfer t[2];
  119. struct spi_message m;
  120. int err;
  121. spi_message_init(&m);
  122. memset(&t, 0, sizeof(t));
  123. t[0].tx_buf = cmd;
  124. t[0].len = sizeof(cmd);
  125. spi_message_add_tail(&t[0], &m);
  126. t[1].tx_buf = buf;
  127. t[1].len = count;
  128. spi_message_add_tail(&t[1], &m);
  129. cmd[0] = KS8995_CMD_WRITE;
  130. cmd[1] = offset;
  131. mutex_lock(&ks->lock);
  132. err = spi_sync(ks->spi, &m);
  133. mutex_unlock(&ks->lock);
  134. return err ? err : count;
  135. }
  136. static inline int ks8995_read_reg(struct ks8995_switch *ks, u8 addr, u8 *buf)
  137. {
  138. return ks8995_read(ks, buf, addr, 1) != 1;
  139. }
  140. static inline int ks8995_write_reg(struct ks8995_switch *ks, u8 addr, u8 val)
  141. {
  142. char buf = val;
  143. return ks8995_write(ks, &buf, addr, 1) != 1;
  144. }
  145. /* ------------------------------------------------------------------------ */
  146. static int ks8995_stop(struct ks8995_switch *ks)
  147. {
  148. return ks8995_write_reg(ks, KS8995_REG_ID1, 0);
  149. }
  150. static int ks8995_start(struct ks8995_switch *ks)
  151. {
  152. return ks8995_write_reg(ks, KS8995_REG_ID1, 1);
  153. }
  154. static int ks8995_reset(struct ks8995_switch *ks)
  155. {
  156. int err;
  157. err = ks8995_stop(ks);
  158. if (err)
  159. return err;
  160. udelay(KS8995_RESET_DELAY);
  161. return ks8995_start(ks);
  162. }
  163. static ssize_t ks8995_registers_read(struct file *filp, struct kobject *kobj,
  164. struct bin_attribute *bin_attr, char *buf, loff_t off, size_t count)
  165. {
  166. struct device *dev;
  167. struct ks8995_switch *ks8995;
  168. dev = container_of(kobj, struct device, kobj);
  169. ks8995 = dev_get_drvdata(dev);
  170. return ks8995_read(ks8995, buf, off, count);
  171. }
  172. static ssize_t ks8995_registers_write(struct file *filp, struct kobject *kobj,
  173. struct bin_attribute *bin_attr, char *buf, loff_t off, size_t count)
  174. {
  175. struct device *dev;
  176. struct ks8995_switch *ks8995;
  177. dev = container_of(kobj, struct device, kobj);
  178. ks8995 = dev_get_drvdata(dev);
  179. return ks8995_write(ks8995, buf, off, count);
  180. }
  181. static const struct bin_attribute ks8995_registers_attr = {
  182. .attr = {
  183. .name = "registers",
  184. .mode = S_IRUSR | S_IWUSR,
  185. },
  186. .size = KS8995_REGS_SIZE,
  187. .read = ks8995_registers_read,
  188. .write = ks8995_registers_write,
  189. };
  190. /* ------------------------------------------------------------------------ */
  191. static int ks8995_probe(struct spi_device *spi)
  192. {
  193. struct ks8995_switch *ks;
  194. struct ks8995_pdata *pdata;
  195. u8 ids[2];
  196. int err;
  197. /* Chip description */
  198. pdata = spi->dev.platform_data;
  199. ks = devm_kzalloc(&spi->dev, sizeof(*ks), GFP_KERNEL);
  200. if (!ks)
  201. return -ENOMEM;
  202. mutex_init(&ks->lock);
  203. ks->pdata = pdata;
  204. ks->spi = spi_dev_get(spi);
  205. spi_set_drvdata(spi, ks);
  206. spi->mode = SPI_MODE_0;
  207. spi->bits_per_word = 8;
  208. err = spi_setup(spi);
  209. if (err) {
  210. dev_err(&spi->dev, "spi_setup failed, err=%d\n", err);
  211. return err;
  212. }
  213. err = ks8995_read(ks, ids, KS8995_REG_ID0, sizeof(ids));
  214. if (err < 0) {
  215. dev_err(&spi->dev, "unable to read id registers, err=%d\n",
  216. err);
  217. return err;
  218. }
  219. switch (ids[0]) {
  220. case FAMILY_KS8995:
  221. break;
  222. default:
  223. dev_err(&spi->dev, "unknown family id:%02x\n", ids[0]);
  224. return -ENODEV;
  225. }
  226. memcpy(&ks->regs_attr, &ks8995_registers_attr, sizeof(ks->regs_attr));
  227. if (get_chip_id(ids[1]) != CHIPID_M) {
  228. u8 val;
  229. /* Check if this is a KSZ8864RMN */
  230. err = ks8995_read(ks, &val, KSZ8864_REG_ID1, sizeof(val));
  231. if (err < 0) {
  232. dev_err(&spi->dev,
  233. "unable to read chip id register, err=%d\n",
  234. err);
  235. return err;
  236. }
  237. if ((val & 0x80) == 0) {
  238. dev_err(&spi->dev, "unknown chip:%02x,0\n", ids[1]);
  239. return err;
  240. }
  241. ks->regs_attr.size = KSZ8864_REGS_SIZE;
  242. }
  243. err = ks8995_reset(ks);
  244. if (err)
  245. return err;
  246. sysfs_attr_init(&ks->regs_attr.attr);
  247. err = sysfs_create_bin_file(&spi->dev.kobj, &ks->regs_attr);
  248. if (err) {
  249. dev_err(&spi->dev, "unable to create sysfs file, err=%d\n",
  250. err);
  251. return err;
  252. }
  253. if (get_chip_id(ids[1]) == CHIPID_M) {
  254. dev_info(&spi->dev,
  255. "KS8995 device found, Chip ID:%x, Revision:%x\n",
  256. get_chip_id(ids[1]), get_chip_rev(ids[1]));
  257. } else {
  258. dev_info(&spi->dev, "KSZ8864 device found, Revision:%x\n",
  259. get_chip_rev(ids[1]));
  260. }
  261. return 0;
  262. }
  263. static int ks8995_remove(struct spi_device *spi)
  264. {
  265. struct ks8995_switch *ks = spi_get_drvdata(spi);
  266. sysfs_remove_bin_file(&spi->dev.kobj, &ks->regs_attr);
  267. return 0;
  268. }
  269. /* ------------------------------------------------------------------------ */
  270. static struct spi_driver ks8995_driver = {
  271. .driver = {
  272. .name = "spi-ks8995",
  273. },
  274. .probe = ks8995_probe,
  275. .remove = ks8995_remove,
  276. };
  277. module_spi_driver(ks8995_driver);
  278. MODULE_DESCRIPTION(DRV_DESC);
  279. MODULE_VERSION(DRV_VERSION);
  280. MODULE_AUTHOR("Gabor Juhos <juhosg at openwrt.org>");
  281. MODULE_LICENSE("GPL v2");