spi-fsl-spi.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. /*
  2. * Freescale SPI controller driver.
  3. *
  4. * Maintainer: Kumar Gala
  5. *
  6. * Copyright (C) 2006 Polycom, Inc.
  7. * Copyright 2010 Freescale Semiconductor, Inc.
  8. *
  9. * CPM SPI and QE buffer descriptors mode support:
  10. * Copyright (c) 2009 MontaVista Software, Inc.
  11. * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
  12. *
  13. * This program is free software; you can redistribute it and/or modify it
  14. * under the terms of the GNU General Public License as published by the
  15. * Free Software Foundation; either version 2 of the License, or (at your
  16. * option) any later version.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/types.h>
  20. #include <linux/kernel.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/delay.h>
  23. #include <linux/irq.h>
  24. #include <linux/spi/spi.h>
  25. #include <linux/spi/spi_bitbang.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/fsl_devices.h>
  28. #include <linux/dma-mapping.h>
  29. #include <linux/mm.h>
  30. #include <linux/mutex.h>
  31. #include <linux/of.h>
  32. #include <linux/of_platform.h>
  33. #include <linux/of_address.h>
  34. #include <linux/of_irq.h>
  35. #include <linux/gpio.h>
  36. #include <linux/of_gpio.h>
  37. #include "spi-fsl-lib.h"
  38. #include "spi-fsl-cpm.h"
  39. #include "spi-fsl-spi.h"
  40. #define TYPE_FSL 0
  41. struct fsl_spi_match_data {
  42. int type;
  43. };
  44. static struct fsl_spi_match_data of_fsl_spi_fsl_config = {
  45. .type = TYPE_FSL,
  46. };
  47. static struct of_device_id of_fsl_spi_match[] = {
  48. {
  49. .compatible = "fsl,spi",
  50. .data = &of_fsl_spi_fsl_config,
  51. },
  52. {}
  53. };
  54. MODULE_DEVICE_TABLE(of, of_fsl_spi_match);
  55. static int fsl_spi_get_type(struct device *dev)
  56. {
  57. const struct of_device_id *match;
  58. if (dev->of_node) {
  59. match = of_match_node(of_fsl_spi_match, dev->of_node);
  60. if (match && match->data)
  61. return ((struct fsl_spi_match_data *)match->data)->type;
  62. }
  63. return TYPE_FSL;
  64. }
  65. static void fsl_spi_change_mode(struct spi_device *spi)
  66. {
  67. struct mpc8xxx_spi *mspi = spi_master_get_devdata(spi->master);
  68. struct spi_mpc8xxx_cs *cs = spi->controller_state;
  69. struct fsl_spi_reg *reg_base = mspi->reg_base;
  70. __be32 __iomem *mode = &reg_base->mode;
  71. unsigned long flags;
  72. if (cs->hw_mode == mpc8xxx_spi_read_reg(mode))
  73. return;
  74. /* Turn off IRQs locally to minimize time that SPI is disabled. */
  75. local_irq_save(flags);
  76. /* Turn off SPI unit prior changing mode */
  77. mpc8xxx_spi_write_reg(mode, cs->hw_mode & ~SPMODE_ENABLE);
  78. /* When in CPM mode, we need to reinit tx and rx. */
  79. if (mspi->flags & SPI_CPM_MODE) {
  80. fsl_spi_cpm_reinit_txrx(mspi);
  81. }
  82. mpc8xxx_spi_write_reg(mode, cs->hw_mode);
  83. local_irq_restore(flags);
  84. }
  85. static void fsl_spi_chipselect(struct spi_device *spi, int value)
  86. {
  87. struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
  88. struct fsl_spi_platform_data *pdata;
  89. bool pol = spi->mode & SPI_CS_HIGH;
  90. struct spi_mpc8xxx_cs *cs = spi->controller_state;
  91. pdata = spi->dev.parent->parent->platform_data;
  92. if (value == BITBANG_CS_INACTIVE) {
  93. if (pdata->cs_control)
  94. pdata->cs_control(spi, !pol);
  95. }
  96. if (value == BITBANG_CS_ACTIVE) {
  97. mpc8xxx_spi->rx_shift = cs->rx_shift;
  98. mpc8xxx_spi->tx_shift = cs->tx_shift;
  99. mpc8xxx_spi->get_rx = cs->get_rx;
  100. mpc8xxx_spi->get_tx = cs->get_tx;
  101. fsl_spi_change_mode(spi);
  102. if (pdata->cs_control)
  103. pdata->cs_control(spi, pol);
  104. }
  105. }
  106. static void fsl_spi_qe_cpu_set_shifts(u32 *rx_shift, u32 *tx_shift,
  107. int bits_per_word, int msb_first)
  108. {
  109. *rx_shift = 0;
  110. *tx_shift = 0;
  111. if (msb_first) {
  112. if (bits_per_word <= 8) {
  113. *rx_shift = 16;
  114. *tx_shift = 24;
  115. } else if (bits_per_word <= 16) {
  116. *rx_shift = 16;
  117. *tx_shift = 16;
  118. }
  119. } else {
  120. if (bits_per_word <= 8)
  121. *rx_shift = 8;
  122. }
  123. }
  124. static int mspi_apply_cpu_mode_quirks(struct spi_mpc8xxx_cs *cs,
  125. struct spi_device *spi,
  126. struct mpc8xxx_spi *mpc8xxx_spi,
  127. int bits_per_word)
  128. {
  129. cs->rx_shift = 0;
  130. cs->tx_shift = 0;
  131. if (bits_per_word <= 8) {
  132. cs->get_rx = mpc8xxx_spi_rx_buf_u8;
  133. cs->get_tx = mpc8xxx_spi_tx_buf_u8;
  134. } else if (bits_per_word <= 16) {
  135. cs->get_rx = mpc8xxx_spi_rx_buf_u16;
  136. cs->get_tx = mpc8xxx_spi_tx_buf_u16;
  137. } else if (bits_per_word <= 32) {
  138. cs->get_rx = mpc8xxx_spi_rx_buf_u32;
  139. cs->get_tx = mpc8xxx_spi_tx_buf_u32;
  140. } else
  141. return -EINVAL;
  142. if (mpc8xxx_spi->set_shifts)
  143. mpc8xxx_spi->set_shifts(&cs->rx_shift, &cs->tx_shift,
  144. bits_per_word,
  145. !(spi->mode & SPI_LSB_FIRST));
  146. mpc8xxx_spi->rx_shift = cs->rx_shift;
  147. mpc8xxx_spi->tx_shift = cs->tx_shift;
  148. mpc8xxx_spi->get_rx = cs->get_rx;
  149. mpc8xxx_spi->get_tx = cs->get_tx;
  150. return bits_per_word;
  151. }
  152. static int mspi_apply_qe_mode_quirks(struct spi_mpc8xxx_cs *cs,
  153. struct spi_device *spi,
  154. int bits_per_word)
  155. {
  156. /* QE uses Little Endian for words > 8
  157. * so transform all words > 8 into 8 bits
  158. * Unfortnatly that doesn't work for LSB so
  159. * reject these for now */
  160. /* Note: 32 bits word, LSB works iff
  161. * tfcr/rfcr is set to CPMFCR_GBL */
  162. if (spi->mode & SPI_LSB_FIRST &&
  163. bits_per_word > 8)
  164. return -EINVAL;
  165. if (bits_per_word > 8)
  166. return 8; /* pretend its 8 bits */
  167. return bits_per_word;
  168. }
  169. static int fsl_spi_setup_transfer(struct spi_device *spi,
  170. struct spi_transfer *t)
  171. {
  172. struct mpc8xxx_spi *mpc8xxx_spi;
  173. int bits_per_word = 0;
  174. u8 pm;
  175. u32 hz = 0;
  176. struct spi_mpc8xxx_cs *cs = spi->controller_state;
  177. mpc8xxx_spi = spi_master_get_devdata(spi->master);
  178. if (t) {
  179. bits_per_word = t->bits_per_word;
  180. hz = t->speed_hz;
  181. }
  182. /* spi_transfer level calls that work per-word */
  183. if (!bits_per_word)
  184. bits_per_word = spi->bits_per_word;
  185. /* Make sure its a bit width we support [4..16, 32] */
  186. if ((bits_per_word < 4)
  187. || ((bits_per_word > 16) && (bits_per_word != 32))
  188. || (bits_per_word > mpc8xxx_spi->max_bits_per_word))
  189. return -EINVAL;
  190. if (!hz)
  191. hz = spi->max_speed_hz;
  192. if (!(mpc8xxx_spi->flags & SPI_CPM_MODE))
  193. bits_per_word = mspi_apply_cpu_mode_quirks(cs, spi,
  194. mpc8xxx_spi,
  195. bits_per_word);
  196. else if (mpc8xxx_spi->flags & SPI_QE)
  197. bits_per_word = mspi_apply_qe_mode_quirks(cs, spi,
  198. bits_per_word);
  199. if (bits_per_word < 0)
  200. return bits_per_word;
  201. if (bits_per_word == 32)
  202. bits_per_word = 0;
  203. else
  204. bits_per_word = bits_per_word - 1;
  205. /* mask out bits we are going to set */
  206. cs->hw_mode &= ~(SPMODE_LEN(0xF) | SPMODE_DIV16
  207. | SPMODE_PM(0xF));
  208. cs->hw_mode |= SPMODE_LEN(bits_per_word);
  209. if ((mpc8xxx_spi->spibrg / hz) > 64) {
  210. cs->hw_mode |= SPMODE_DIV16;
  211. pm = (mpc8xxx_spi->spibrg - 1) / (hz * 64) + 1;
  212. WARN_ONCE(pm > 16, "%s: Requested speed is too low: %d Hz. "
  213. "Will use %d Hz instead.\n", dev_name(&spi->dev),
  214. hz, mpc8xxx_spi->spibrg / 1024);
  215. if (pm > 16)
  216. pm = 16;
  217. } else {
  218. pm = (mpc8xxx_spi->spibrg - 1) / (hz * 4) + 1;
  219. }
  220. if (pm)
  221. pm--;
  222. cs->hw_mode |= SPMODE_PM(pm);
  223. fsl_spi_change_mode(spi);
  224. return 0;
  225. }
  226. static int fsl_spi_cpu_bufs(struct mpc8xxx_spi *mspi,
  227. struct spi_transfer *t, unsigned int len)
  228. {
  229. u32 word;
  230. struct fsl_spi_reg *reg_base = mspi->reg_base;
  231. mspi->count = len;
  232. /* enable rx ints */
  233. mpc8xxx_spi_write_reg(&reg_base->mask, SPIM_NE);
  234. /* transmit word */
  235. word = mspi->get_tx(mspi);
  236. mpc8xxx_spi_write_reg(&reg_base->transmit, word);
  237. return 0;
  238. }
  239. static int fsl_spi_bufs(struct spi_device *spi, struct spi_transfer *t,
  240. bool is_dma_mapped)
  241. {
  242. struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
  243. struct fsl_spi_reg *reg_base;
  244. unsigned int len = t->len;
  245. u8 bits_per_word;
  246. int ret;
  247. reg_base = mpc8xxx_spi->reg_base;
  248. bits_per_word = spi->bits_per_word;
  249. if (t->bits_per_word)
  250. bits_per_word = t->bits_per_word;
  251. if (bits_per_word > 8) {
  252. /* invalid length? */
  253. if (len & 1)
  254. return -EINVAL;
  255. len /= 2;
  256. }
  257. if (bits_per_word > 16) {
  258. /* invalid length? */
  259. if (len & 1)
  260. return -EINVAL;
  261. len /= 2;
  262. }
  263. mpc8xxx_spi->tx = t->tx_buf;
  264. mpc8xxx_spi->rx = t->rx_buf;
  265. INIT_COMPLETION(mpc8xxx_spi->done);
  266. if (mpc8xxx_spi->flags & SPI_CPM_MODE)
  267. ret = fsl_spi_cpm_bufs(mpc8xxx_spi, t, is_dma_mapped);
  268. else
  269. ret = fsl_spi_cpu_bufs(mpc8xxx_spi, t, len);
  270. if (ret)
  271. return ret;
  272. wait_for_completion(&mpc8xxx_spi->done);
  273. /* disable rx ints */
  274. mpc8xxx_spi_write_reg(&reg_base->mask, 0);
  275. if (mpc8xxx_spi->flags & SPI_CPM_MODE)
  276. fsl_spi_cpm_bufs_complete(mpc8xxx_spi);
  277. return mpc8xxx_spi->count;
  278. }
  279. static void fsl_spi_do_one_msg(struct spi_message *m)
  280. {
  281. struct spi_device *spi = m->spi;
  282. struct spi_transfer *t;
  283. unsigned int cs_change;
  284. const int nsecs = 50;
  285. int status;
  286. cs_change = 1;
  287. status = 0;
  288. list_for_each_entry(t, &m->transfers, transfer_list) {
  289. if (t->bits_per_word || t->speed_hz) {
  290. /* Don't allow changes if CS is active */
  291. status = -EINVAL;
  292. if (cs_change)
  293. status = fsl_spi_setup_transfer(spi, t);
  294. if (status < 0)
  295. break;
  296. }
  297. if (cs_change) {
  298. fsl_spi_chipselect(spi, BITBANG_CS_ACTIVE);
  299. ndelay(nsecs);
  300. }
  301. cs_change = t->cs_change;
  302. if (t->len)
  303. status = fsl_spi_bufs(spi, t, m->is_dma_mapped);
  304. if (status) {
  305. status = -EMSGSIZE;
  306. break;
  307. }
  308. m->actual_length += t->len;
  309. if (t->delay_usecs)
  310. udelay(t->delay_usecs);
  311. if (cs_change) {
  312. ndelay(nsecs);
  313. fsl_spi_chipselect(spi, BITBANG_CS_INACTIVE);
  314. ndelay(nsecs);
  315. }
  316. }
  317. m->status = status;
  318. m->complete(m->context);
  319. if (status || !cs_change) {
  320. ndelay(nsecs);
  321. fsl_spi_chipselect(spi, BITBANG_CS_INACTIVE);
  322. }
  323. fsl_spi_setup_transfer(spi, NULL);
  324. }
  325. static int fsl_spi_setup(struct spi_device *spi)
  326. {
  327. struct mpc8xxx_spi *mpc8xxx_spi;
  328. struct fsl_spi_reg *reg_base;
  329. int retval;
  330. u32 hw_mode;
  331. struct spi_mpc8xxx_cs *cs = spi->controller_state;
  332. if (!spi->max_speed_hz)
  333. return -EINVAL;
  334. if (!cs) {
  335. cs = kzalloc(sizeof *cs, GFP_KERNEL);
  336. if (!cs)
  337. return -ENOMEM;
  338. spi->controller_state = cs;
  339. }
  340. mpc8xxx_spi = spi_master_get_devdata(spi->master);
  341. reg_base = mpc8xxx_spi->reg_base;
  342. hw_mode = cs->hw_mode; /* Save original settings */
  343. cs->hw_mode = mpc8xxx_spi_read_reg(&reg_base->mode);
  344. /* mask out bits we are going to set */
  345. cs->hw_mode &= ~(SPMODE_CP_BEGIN_EDGECLK | SPMODE_CI_INACTIVEHIGH
  346. | SPMODE_REV | SPMODE_LOOP);
  347. if (spi->mode & SPI_CPHA)
  348. cs->hw_mode |= SPMODE_CP_BEGIN_EDGECLK;
  349. if (spi->mode & SPI_CPOL)
  350. cs->hw_mode |= SPMODE_CI_INACTIVEHIGH;
  351. if (!(spi->mode & SPI_LSB_FIRST))
  352. cs->hw_mode |= SPMODE_REV;
  353. if (spi->mode & SPI_LOOP)
  354. cs->hw_mode |= SPMODE_LOOP;
  355. retval = fsl_spi_setup_transfer(spi, NULL);
  356. if (retval < 0) {
  357. cs->hw_mode = hw_mode; /* Restore settings */
  358. return retval;
  359. }
  360. /* Initialize chipselect - might be active for SPI_CS_HIGH mode */
  361. fsl_spi_chipselect(spi, BITBANG_CS_INACTIVE);
  362. return 0;
  363. }
  364. static void fsl_spi_cpu_irq(struct mpc8xxx_spi *mspi, u32 events)
  365. {
  366. struct fsl_spi_reg *reg_base = mspi->reg_base;
  367. /* We need handle RX first */
  368. if (events & SPIE_NE) {
  369. u32 rx_data = mpc8xxx_spi_read_reg(&reg_base->receive);
  370. if (mspi->rx)
  371. mspi->get_rx(rx_data, mspi);
  372. }
  373. if ((events & SPIE_NF) == 0)
  374. /* spin until TX is done */
  375. while (((events =
  376. mpc8xxx_spi_read_reg(&reg_base->event)) &
  377. SPIE_NF) == 0)
  378. cpu_relax();
  379. /* Clear the events */
  380. mpc8xxx_spi_write_reg(&reg_base->event, events);
  381. mspi->count -= 1;
  382. if (mspi->count) {
  383. u32 word = mspi->get_tx(mspi);
  384. mpc8xxx_spi_write_reg(&reg_base->transmit, word);
  385. } else {
  386. complete(&mspi->done);
  387. }
  388. }
  389. static irqreturn_t fsl_spi_irq(s32 irq, void *context_data)
  390. {
  391. struct mpc8xxx_spi *mspi = context_data;
  392. irqreturn_t ret = IRQ_NONE;
  393. u32 events;
  394. struct fsl_spi_reg *reg_base = mspi->reg_base;
  395. /* Get interrupt events(tx/rx) */
  396. events = mpc8xxx_spi_read_reg(&reg_base->event);
  397. if (events)
  398. ret = IRQ_HANDLED;
  399. dev_dbg(mspi->dev, "%s: events %x\n", __func__, events);
  400. if (mspi->flags & SPI_CPM_MODE)
  401. fsl_spi_cpm_irq(mspi, events);
  402. else
  403. fsl_spi_cpu_irq(mspi, events);
  404. return ret;
  405. }
  406. static void fsl_spi_remove(struct mpc8xxx_spi *mspi)
  407. {
  408. iounmap(mspi->reg_base);
  409. fsl_spi_cpm_free(mspi);
  410. }
  411. static struct spi_master * fsl_spi_probe(struct device *dev,
  412. struct resource *mem, unsigned int irq)
  413. {
  414. struct fsl_spi_platform_data *pdata = dev->platform_data;
  415. struct spi_master *master;
  416. struct mpc8xxx_spi *mpc8xxx_spi;
  417. struct fsl_spi_reg *reg_base;
  418. u32 regval;
  419. int ret = 0;
  420. master = spi_alloc_master(dev, sizeof(struct mpc8xxx_spi));
  421. if (master == NULL) {
  422. ret = -ENOMEM;
  423. goto err;
  424. }
  425. dev_set_drvdata(dev, master);
  426. ret = mpc8xxx_spi_probe(dev, mem, irq);
  427. if (ret)
  428. goto err_probe;
  429. master->setup = fsl_spi_setup;
  430. mpc8xxx_spi = spi_master_get_devdata(master);
  431. mpc8xxx_spi->spi_do_one_msg = fsl_spi_do_one_msg;
  432. mpc8xxx_spi->spi_remove = fsl_spi_remove;
  433. mpc8xxx_spi->max_bits_per_word = 32;
  434. mpc8xxx_spi->type = fsl_spi_get_type(dev);
  435. ret = fsl_spi_cpm_init(mpc8xxx_spi);
  436. if (ret)
  437. goto err_cpm_init;
  438. if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE)
  439. mpc8xxx_spi->set_shifts = fsl_spi_qe_cpu_set_shifts;
  440. if (mpc8xxx_spi->set_shifts)
  441. /* 8 bits per word and MSB first */
  442. mpc8xxx_spi->set_shifts(&mpc8xxx_spi->rx_shift,
  443. &mpc8xxx_spi->tx_shift, 8, 1);
  444. mpc8xxx_spi->reg_base = ioremap(mem->start, resource_size(mem));
  445. if (mpc8xxx_spi->reg_base == NULL) {
  446. ret = -ENOMEM;
  447. goto err_ioremap;
  448. }
  449. /* Register for SPI Interrupt */
  450. ret = request_irq(mpc8xxx_spi->irq, fsl_spi_irq,
  451. 0, "fsl_spi", mpc8xxx_spi);
  452. if (ret != 0)
  453. goto free_irq;
  454. reg_base = mpc8xxx_spi->reg_base;
  455. /* SPI controller initializations */
  456. mpc8xxx_spi_write_reg(&reg_base->mode, 0);
  457. mpc8xxx_spi_write_reg(&reg_base->mask, 0);
  458. mpc8xxx_spi_write_reg(&reg_base->command, 0);
  459. mpc8xxx_spi_write_reg(&reg_base->event, 0xffffffff);
  460. /* Enable SPI interface */
  461. regval = pdata->initial_spmode | SPMODE_INIT_VAL | SPMODE_ENABLE;
  462. if (mpc8xxx_spi->max_bits_per_word < 8) {
  463. regval &= ~SPMODE_LEN(0xF);
  464. regval |= SPMODE_LEN(mpc8xxx_spi->max_bits_per_word - 1);
  465. }
  466. if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE)
  467. regval |= SPMODE_OP;
  468. mpc8xxx_spi_write_reg(&reg_base->mode, regval);
  469. ret = spi_register_master(master);
  470. if (ret < 0)
  471. goto unreg_master;
  472. dev_info(dev, "at 0x%p (irq = %d), %s mode\n", reg_base,
  473. mpc8xxx_spi->irq, mpc8xxx_spi_strmode(mpc8xxx_spi->flags));
  474. return master;
  475. unreg_master:
  476. free_irq(mpc8xxx_spi->irq, mpc8xxx_spi);
  477. free_irq:
  478. iounmap(mpc8xxx_spi->reg_base);
  479. err_ioremap:
  480. fsl_spi_cpm_free(mpc8xxx_spi);
  481. err_cpm_init:
  482. err_probe:
  483. spi_master_put(master);
  484. err:
  485. return ERR_PTR(ret);
  486. }
  487. static void fsl_spi_cs_control(struct spi_device *spi, bool on)
  488. {
  489. struct device *dev = spi->dev.parent->parent;
  490. struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(dev->platform_data);
  491. u16 cs = spi->chip_select;
  492. int gpio = pinfo->gpios[cs];
  493. bool alow = pinfo->alow_flags[cs];
  494. gpio_set_value(gpio, on ^ alow);
  495. }
  496. static int of_fsl_spi_get_chipselects(struct device *dev)
  497. {
  498. struct device_node *np = dev->of_node;
  499. struct fsl_spi_platform_data *pdata = dev->platform_data;
  500. struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
  501. int ngpios;
  502. int i = 0;
  503. int ret;
  504. ngpios = of_gpio_count(np);
  505. if (ngpios <= 0) {
  506. /*
  507. * SPI w/o chip-select line. One SPI device is still permitted
  508. * though.
  509. */
  510. pdata->max_chipselect = 1;
  511. return 0;
  512. }
  513. pinfo->gpios = kmalloc(ngpios * sizeof(*pinfo->gpios), GFP_KERNEL);
  514. if (!pinfo->gpios)
  515. return -ENOMEM;
  516. memset(pinfo->gpios, -1, ngpios * sizeof(*pinfo->gpios));
  517. pinfo->alow_flags = kzalloc(ngpios * sizeof(*pinfo->alow_flags),
  518. GFP_KERNEL);
  519. if (!pinfo->alow_flags) {
  520. ret = -ENOMEM;
  521. goto err_alloc_flags;
  522. }
  523. for (; i < ngpios; i++) {
  524. int gpio;
  525. enum of_gpio_flags flags;
  526. gpio = of_get_gpio_flags(np, i, &flags);
  527. if (!gpio_is_valid(gpio)) {
  528. dev_err(dev, "invalid gpio #%d: %d\n", i, gpio);
  529. ret = gpio;
  530. goto err_loop;
  531. }
  532. ret = gpio_request(gpio, dev_name(dev));
  533. if (ret) {
  534. dev_err(dev, "can't request gpio #%d: %d\n", i, ret);
  535. goto err_loop;
  536. }
  537. pinfo->gpios[i] = gpio;
  538. pinfo->alow_flags[i] = flags & OF_GPIO_ACTIVE_LOW;
  539. ret = gpio_direction_output(pinfo->gpios[i],
  540. pinfo->alow_flags[i]);
  541. if (ret) {
  542. dev_err(dev, "can't set output direction for gpio "
  543. "#%d: %d\n", i, ret);
  544. goto err_loop;
  545. }
  546. }
  547. pdata->max_chipselect = ngpios;
  548. pdata->cs_control = fsl_spi_cs_control;
  549. return 0;
  550. err_loop:
  551. while (i >= 0) {
  552. if (gpio_is_valid(pinfo->gpios[i]))
  553. gpio_free(pinfo->gpios[i]);
  554. i--;
  555. }
  556. kfree(pinfo->alow_flags);
  557. pinfo->alow_flags = NULL;
  558. err_alloc_flags:
  559. kfree(pinfo->gpios);
  560. pinfo->gpios = NULL;
  561. return ret;
  562. }
  563. static int of_fsl_spi_free_chipselects(struct device *dev)
  564. {
  565. struct fsl_spi_platform_data *pdata = dev->platform_data;
  566. struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
  567. int i;
  568. if (!pinfo->gpios)
  569. return 0;
  570. for (i = 0; i < pdata->max_chipselect; i++) {
  571. if (gpio_is_valid(pinfo->gpios[i]))
  572. gpio_free(pinfo->gpios[i]);
  573. }
  574. kfree(pinfo->gpios);
  575. kfree(pinfo->alow_flags);
  576. return 0;
  577. }
  578. static int of_fsl_spi_probe(struct platform_device *ofdev)
  579. {
  580. struct device *dev = &ofdev->dev;
  581. struct device_node *np = ofdev->dev.of_node;
  582. struct spi_master *master;
  583. struct resource mem;
  584. int irq;
  585. int ret = -ENOMEM;
  586. ret = of_mpc8xxx_spi_probe(ofdev);
  587. if (ret)
  588. return ret;
  589. ret = of_fsl_spi_get_chipselects(dev);
  590. if (ret)
  591. goto err;
  592. ret = of_address_to_resource(np, 0, &mem);
  593. if (ret)
  594. goto err;
  595. irq = irq_of_parse_and_map(np, 0);
  596. if (!irq) {
  597. ret = -EINVAL;
  598. goto err;
  599. }
  600. master = fsl_spi_probe(dev, &mem, irq);
  601. if (IS_ERR(master)) {
  602. ret = PTR_ERR(master);
  603. goto err;
  604. }
  605. return 0;
  606. err:
  607. of_fsl_spi_free_chipselects(dev);
  608. return ret;
  609. }
  610. static int of_fsl_spi_remove(struct platform_device *ofdev)
  611. {
  612. int ret;
  613. ret = mpc8xxx_spi_remove(&ofdev->dev);
  614. if (ret)
  615. return ret;
  616. of_fsl_spi_free_chipselects(&ofdev->dev);
  617. return 0;
  618. }
  619. static struct platform_driver of_fsl_spi_driver = {
  620. .driver = {
  621. .name = "fsl_spi",
  622. .owner = THIS_MODULE,
  623. .of_match_table = of_fsl_spi_match,
  624. },
  625. .probe = of_fsl_spi_probe,
  626. .remove = of_fsl_spi_remove,
  627. };
  628. #ifdef CONFIG_MPC832x_RDB
  629. /*
  630. * XXX XXX XXX
  631. * This is "legacy" platform driver, was used by the MPC8323E-RDB boards
  632. * only. The driver should go away soon, since newer MPC8323E-RDB's device
  633. * tree can work with OpenFirmware driver. But for now we support old trees
  634. * as well.
  635. */
  636. static int plat_mpc8xxx_spi_probe(struct platform_device *pdev)
  637. {
  638. struct resource *mem;
  639. int irq;
  640. struct spi_master *master;
  641. if (!pdev->dev.platform_data)
  642. return -EINVAL;
  643. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  644. if (!mem)
  645. return -EINVAL;
  646. irq = platform_get_irq(pdev, 0);
  647. if (irq <= 0)
  648. return -EINVAL;
  649. master = fsl_spi_probe(&pdev->dev, mem, irq);
  650. return PTR_RET(master);
  651. }
  652. static int plat_mpc8xxx_spi_remove(struct platform_device *pdev)
  653. {
  654. return mpc8xxx_spi_remove(&pdev->dev);
  655. }
  656. MODULE_ALIAS("platform:mpc8xxx_spi");
  657. static struct platform_driver mpc8xxx_spi_driver = {
  658. .probe = plat_mpc8xxx_spi_probe,
  659. .remove = plat_mpc8xxx_spi_remove,
  660. .driver = {
  661. .name = "mpc8xxx_spi",
  662. .owner = THIS_MODULE,
  663. },
  664. };
  665. static bool legacy_driver_failed;
  666. static void __init legacy_driver_register(void)
  667. {
  668. legacy_driver_failed = platform_driver_register(&mpc8xxx_spi_driver);
  669. }
  670. static void __exit legacy_driver_unregister(void)
  671. {
  672. if (legacy_driver_failed)
  673. return;
  674. platform_driver_unregister(&mpc8xxx_spi_driver);
  675. }
  676. #else
  677. static void __init legacy_driver_register(void) {}
  678. static void __exit legacy_driver_unregister(void) {}
  679. #endif /* CONFIG_MPC832x_RDB */
  680. static int __init fsl_spi_init(void)
  681. {
  682. legacy_driver_register();
  683. return platform_driver_register(&of_fsl_spi_driver);
  684. }
  685. module_init(fsl_spi_init);
  686. static void __exit fsl_spi_exit(void)
  687. {
  688. platform_driver_unregister(&of_fsl_spi_driver);
  689. legacy_driver_unregister();
  690. }
  691. module_exit(fsl_spi_exit);
  692. MODULE_AUTHOR("Kumar Gala");
  693. MODULE_DESCRIPTION("Simple Freescale SPI Driver");
  694. MODULE_LICENSE("GPL");