spi-fsl-spi.c 21 KB

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