spi-pxa2xx.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749
  1. /*
  2. * Copyright (C) 2005 Stephen Street / StreetFire Sound Labs
  3. * Copyright (C) 2013, Intel Corporation
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/bitops.h>
  16. #include <linux/init.h>
  17. #include <linux/module.h>
  18. #include <linux/device.h>
  19. #include <linux/ioport.h>
  20. #include <linux/errno.h>
  21. #include <linux/err.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/kernel.h>
  24. #include <linux/pci.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/spi/pxa2xx_spi.h>
  27. #include <linux/spi/spi.h>
  28. #include <linux/delay.h>
  29. #include <linux/gpio.h>
  30. #include <linux/slab.h>
  31. #include <linux/clk.h>
  32. #include <linux/pm_runtime.h>
  33. #include <linux/acpi.h>
  34. #include "spi-pxa2xx.h"
  35. MODULE_AUTHOR("Stephen Street");
  36. MODULE_DESCRIPTION("PXA2xx SSP SPI Controller");
  37. MODULE_LICENSE("GPL");
  38. MODULE_ALIAS("platform:pxa2xx-spi");
  39. #define TIMOUT_DFLT 1000
  40. /*
  41. * for testing SSCR1 changes that require SSP restart, basically
  42. * everything except the service and interrupt enables, the pxa270 developer
  43. * manual says only SSCR1_SCFR, SSCR1_SPH, SSCR1_SPO need to be in this
  44. * list, but the PXA255 dev man says all bits without really meaning the
  45. * service and interrupt enables
  46. */
  47. #define SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
  48. | SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
  49. | SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
  50. | SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
  51. | SSCR1_RFT | SSCR1_TFT | SSCR1_MWDS \
  52. | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
  53. #define QUARK_X1000_SSCR1_CHANGE_MASK (QUARK_X1000_SSCR1_STRF \
  54. | QUARK_X1000_SSCR1_EFWR \
  55. | QUARK_X1000_SSCR1_RFT \
  56. | QUARK_X1000_SSCR1_TFT \
  57. | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
  58. #define LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE BIT(24)
  59. #define LPSS_CS_CONTROL_SW_MODE BIT(0)
  60. #define LPSS_CS_CONTROL_CS_HIGH BIT(1)
  61. #define LPSS_CS_CONTROL_CS_SEL_SHIFT 8
  62. #define LPSS_CS_CONTROL_CS_SEL_MASK (3 << LPSS_CS_CONTROL_CS_SEL_SHIFT)
  63. #define LPSS_CAPS_CS_EN_SHIFT 9
  64. #define LPSS_CAPS_CS_EN_MASK (0xf << LPSS_CAPS_CS_EN_SHIFT)
  65. struct lpss_config {
  66. /* LPSS offset from drv_data->ioaddr */
  67. unsigned offset;
  68. /* Register offsets from drv_data->lpss_base or -1 */
  69. int reg_general;
  70. int reg_ssp;
  71. int reg_cs_ctrl;
  72. int reg_capabilities;
  73. /* FIFO thresholds */
  74. u32 rx_threshold;
  75. u32 tx_threshold_lo;
  76. u32 tx_threshold_hi;
  77. };
  78. /* Keep these sorted with enum pxa_ssp_type */
  79. static const struct lpss_config lpss_platforms[] = {
  80. { /* LPSS_LPT_SSP */
  81. .offset = 0x800,
  82. .reg_general = 0x08,
  83. .reg_ssp = 0x0c,
  84. .reg_cs_ctrl = 0x18,
  85. .reg_capabilities = -1,
  86. .rx_threshold = 64,
  87. .tx_threshold_lo = 160,
  88. .tx_threshold_hi = 224,
  89. },
  90. { /* LPSS_BYT_SSP */
  91. .offset = 0x400,
  92. .reg_general = 0x08,
  93. .reg_ssp = 0x0c,
  94. .reg_cs_ctrl = 0x18,
  95. .reg_capabilities = -1,
  96. .rx_threshold = 64,
  97. .tx_threshold_lo = 160,
  98. .tx_threshold_hi = 224,
  99. },
  100. { /* LPSS_SPT_SSP */
  101. .offset = 0x200,
  102. .reg_general = -1,
  103. .reg_ssp = 0x20,
  104. .reg_cs_ctrl = 0x24,
  105. .reg_capabilities = -1,
  106. .rx_threshold = 1,
  107. .tx_threshold_lo = 32,
  108. .tx_threshold_hi = 56,
  109. },
  110. { /* LPSS_BXT_SSP */
  111. .offset = 0x200,
  112. .reg_general = -1,
  113. .reg_ssp = 0x20,
  114. .reg_cs_ctrl = 0x24,
  115. .reg_capabilities = 0xfc,
  116. .rx_threshold = 1,
  117. .tx_threshold_lo = 16,
  118. .tx_threshold_hi = 48,
  119. },
  120. };
  121. static inline const struct lpss_config
  122. *lpss_get_config(const struct driver_data *drv_data)
  123. {
  124. return &lpss_platforms[drv_data->ssp_type - LPSS_LPT_SSP];
  125. }
  126. static bool is_lpss_ssp(const struct driver_data *drv_data)
  127. {
  128. switch (drv_data->ssp_type) {
  129. case LPSS_LPT_SSP:
  130. case LPSS_BYT_SSP:
  131. case LPSS_SPT_SSP:
  132. case LPSS_BXT_SSP:
  133. return true;
  134. default:
  135. return false;
  136. }
  137. }
  138. static bool is_quark_x1000_ssp(const struct driver_data *drv_data)
  139. {
  140. return drv_data->ssp_type == QUARK_X1000_SSP;
  141. }
  142. static u32 pxa2xx_spi_get_ssrc1_change_mask(const struct driver_data *drv_data)
  143. {
  144. switch (drv_data->ssp_type) {
  145. case QUARK_X1000_SSP:
  146. return QUARK_X1000_SSCR1_CHANGE_MASK;
  147. default:
  148. return SSCR1_CHANGE_MASK;
  149. }
  150. }
  151. static u32
  152. pxa2xx_spi_get_rx_default_thre(const struct driver_data *drv_data)
  153. {
  154. switch (drv_data->ssp_type) {
  155. case QUARK_X1000_SSP:
  156. return RX_THRESH_QUARK_X1000_DFLT;
  157. default:
  158. return RX_THRESH_DFLT;
  159. }
  160. }
  161. static bool pxa2xx_spi_txfifo_full(const struct driver_data *drv_data)
  162. {
  163. u32 mask;
  164. switch (drv_data->ssp_type) {
  165. case QUARK_X1000_SSP:
  166. mask = QUARK_X1000_SSSR_TFL_MASK;
  167. break;
  168. default:
  169. mask = SSSR_TFL_MASK;
  170. break;
  171. }
  172. return (pxa2xx_spi_read(drv_data, SSSR) & mask) == mask;
  173. }
  174. static void pxa2xx_spi_clear_rx_thre(const struct driver_data *drv_data,
  175. u32 *sccr1_reg)
  176. {
  177. u32 mask;
  178. switch (drv_data->ssp_type) {
  179. case QUARK_X1000_SSP:
  180. mask = QUARK_X1000_SSCR1_RFT;
  181. break;
  182. default:
  183. mask = SSCR1_RFT;
  184. break;
  185. }
  186. *sccr1_reg &= ~mask;
  187. }
  188. static void pxa2xx_spi_set_rx_thre(const struct driver_data *drv_data,
  189. u32 *sccr1_reg, u32 threshold)
  190. {
  191. switch (drv_data->ssp_type) {
  192. case QUARK_X1000_SSP:
  193. *sccr1_reg |= QUARK_X1000_SSCR1_RxTresh(threshold);
  194. break;
  195. default:
  196. *sccr1_reg |= SSCR1_RxTresh(threshold);
  197. break;
  198. }
  199. }
  200. static u32 pxa2xx_configure_sscr0(const struct driver_data *drv_data,
  201. u32 clk_div, u8 bits)
  202. {
  203. switch (drv_data->ssp_type) {
  204. case QUARK_X1000_SSP:
  205. return clk_div
  206. | QUARK_X1000_SSCR0_Motorola
  207. | QUARK_X1000_SSCR0_DataSize(bits > 32 ? 8 : bits)
  208. | SSCR0_SSE;
  209. default:
  210. return clk_div
  211. | SSCR0_Motorola
  212. | SSCR0_DataSize(bits > 16 ? bits - 16 : bits)
  213. | SSCR0_SSE
  214. | (bits > 16 ? SSCR0_EDSS : 0);
  215. }
  216. }
  217. /*
  218. * Read and write LPSS SSP private registers. Caller must first check that
  219. * is_lpss_ssp() returns true before these can be called.
  220. */
  221. static u32 __lpss_ssp_read_priv(struct driver_data *drv_data, unsigned offset)
  222. {
  223. WARN_ON(!drv_data->lpss_base);
  224. return readl(drv_data->lpss_base + offset);
  225. }
  226. static void __lpss_ssp_write_priv(struct driver_data *drv_data,
  227. unsigned offset, u32 value)
  228. {
  229. WARN_ON(!drv_data->lpss_base);
  230. writel(value, drv_data->lpss_base + offset);
  231. }
  232. /*
  233. * lpss_ssp_setup - perform LPSS SSP specific setup
  234. * @drv_data: pointer to the driver private data
  235. *
  236. * Perform LPSS SSP specific setup. This function must be called first if
  237. * one is going to use LPSS SSP private registers.
  238. */
  239. static void lpss_ssp_setup(struct driver_data *drv_data)
  240. {
  241. const struct lpss_config *config;
  242. u32 value;
  243. config = lpss_get_config(drv_data);
  244. drv_data->lpss_base = drv_data->ioaddr + config->offset;
  245. /* Enable software chip select control */
  246. value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
  247. value &= ~(LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH);
  248. value |= LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH;
  249. __lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
  250. /* Enable multiblock DMA transfers */
  251. if (drv_data->master_info->enable_dma) {
  252. __lpss_ssp_write_priv(drv_data, config->reg_ssp, 1);
  253. if (config->reg_general >= 0) {
  254. value = __lpss_ssp_read_priv(drv_data,
  255. config->reg_general);
  256. value |= LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE;
  257. __lpss_ssp_write_priv(drv_data,
  258. config->reg_general, value);
  259. }
  260. }
  261. }
  262. static void lpss_ssp_cs_control(struct driver_data *drv_data, bool enable)
  263. {
  264. const struct lpss_config *config;
  265. u32 value, cs;
  266. config = lpss_get_config(drv_data);
  267. value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
  268. if (enable) {
  269. cs = drv_data->cur_msg->spi->chip_select;
  270. cs <<= LPSS_CS_CONTROL_CS_SEL_SHIFT;
  271. if (cs != (value & LPSS_CS_CONTROL_CS_SEL_MASK)) {
  272. /*
  273. * When switching another chip select output active
  274. * the output must be selected first and wait 2 ssp_clk
  275. * cycles before changing state to active. Otherwise
  276. * a short glitch will occur on the previous chip
  277. * select since output select is latched but state
  278. * control is not.
  279. */
  280. value &= ~LPSS_CS_CONTROL_CS_SEL_MASK;
  281. value |= cs;
  282. __lpss_ssp_write_priv(drv_data,
  283. config->reg_cs_ctrl, value);
  284. ndelay(1000000000 /
  285. (drv_data->master->max_speed_hz / 2));
  286. }
  287. value &= ~LPSS_CS_CONTROL_CS_HIGH;
  288. } else {
  289. value |= LPSS_CS_CONTROL_CS_HIGH;
  290. }
  291. __lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
  292. }
  293. static void cs_assert(struct driver_data *drv_data)
  294. {
  295. struct chip_data *chip = drv_data->cur_chip;
  296. if (drv_data->ssp_type == CE4100_SSP) {
  297. pxa2xx_spi_write(drv_data, SSSR, drv_data->cur_chip->frm);
  298. return;
  299. }
  300. if (chip->cs_control) {
  301. chip->cs_control(PXA2XX_CS_ASSERT);
  302. return;
  303. }
  304. if (gpio_is_valid(chip->gpio_cs)) {
  305. gpio_set_value(chip->gpio_cs, chip->gpio_cs_inverted);
  306. return;
  307. }
  308. if (is_lpss_ssp(drv_data))
  309. lpss_ssp_cs_control(drv_data, true);
  310. }
  311. static void cs_deassert(struct driver_data *drv_data)
  312. {
  313. struct chip_data *chip = drv_data->cur_chip;
  314. if (drv_data->ssp_type == CE4100_SSP)
  315. return;
  316. if (chip->cs_control) {
  317. chip->cs_control(PXA2XX_CS_DEASSERT);
  318. return;
  319. }
  320. if (gpio_is_valid(chip->gpio_cs)) {
  321. gpio_set_value(chip->gpio_cs, !chip->gpio_cs_inverted);
  322. return;
  323. }
  324. if (is_lpss_ssp(drv_data))
  325. lpss_ssp_cs_control(drv_data, false);
  326. }
  327. int pxa2xx_spi_flush(struct driver_data *drv_data)
  328. {
  329. unsigned long limit = loops_per_jiffy << 1;
  330. do {
  331. while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
  332. pxa2xx_spi_read(drv_data, SSDR);
  333. } while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY) && --limit);
  334. write_SSSR_CS(drv_data, SSSR_ROR);
  335. return limit;
  336. }
  337. static int null_writer(struct driver_data *drv_data)
  338. {
  339. u8 n_bytes = drv_data->n_bytes;
  340. if (pxa2xx_spi_txfifo_full(drv_data)
  341. || (drv_data->tx == drv_data->tx_end))
  342. return 0;
  343. pxa2xx_spi_write(drv_data, SSDR, 0);
  344. drv_data->tx += n_bytes;
  345. return 1;
  346. }
  347. static int null_reader(struct driver_data *drv_data)
  348. {
  349. u8 n_bytes = drv_data->n_bytes;
  350. while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
  351. && (drv_data->rx < drv_data->rx_end)) {
  352. pxa2xx_spi_read(drv_data, SSDR);
  353. drv_data->rx += n_bytes;
  354. }
  355. return drv_data->rx == drv_data->rx_end;
  356. }
  357. static int u8_writer(struct driver_data *drv_data)
  358. {
  359. if (pxa2xx_spi_txfifo_full(drv_data)
  360. || (drv_data->tx == drv_data->tx_end))
  361. return 0;
  362. pxa2xx_spi_write(drv_data, SSDR, *(u8 *)(drv_data->tx));
  363. ++drv_data->tx;
  364. return 1;
  365. }
  366. static int u8_reader(struct driver_data *drv_data)
  367. {
  368. while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
  369. && (drv_data->rx < drv_data->rx_end)) {
  370. *(u8 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
  371. ++drv_data->rx;
  372. }
  373. return drv_data->rx == drv_data->rx_end;
  374. }
  375. static int u16_writer(struct driver_data *drv_data)
  376. {
  377. if (pxa2xx_spi_txfifo_full(drv_data)
  378. || (drv_data->tx == drv_data->tx_end))
  379. return 0;
  380. pxa2xx_spi_write(drv_data, SSDR, *(u16 *)(drv_data->tx));
  381. drv_data->tx += 2;
  382. return 1;
  383. }
  384. static int u16_reader(struct driver_data *drv_data)
  385. {
  386. while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
  387. && (drv_data->rx < drv_data->rx_end)) {
  388. *(u16 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
  389. drv_data->rx += 2;
  390. }
  391. return drv_data->rx == drv_data->rx_end;
  392. }
  393. static int u32_writer(struct driver_data *drv_data)
  394. {
  395. if (pxa2xx_spi_txfifo_full(drv_data)
  396. || (drv_data->tx == drv_data->tx_end))
  397. return 0;
  398. pxa2xx_spi_write(drv_data, SSDR, *(u32 *)(drv_data->tx));
  399. drv_data->tx += 4;
  400. return 1;
  401. }
  402. static int u32_reader(struct driver_data *drv_data)
  403. {
  404. while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
  405. && (drv_data->rx < drv_data->rx_end)) {
  406. *(u32 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
  407. drv_data->rx += 4;
  408. }
  409. return drv_data->rx == drv_data->rx_end;
  410. }
  411. void *pxa2xx_spi_next_transfer(struct driver_data *drv_data)
  412. {
  413. struct spi_message *msg = drv_data->cur_msg;
  414. struct spi_transfer *trans = drv_data->cur_transfer;
  415. /* Move to next transfer */
  416. if (trans->transfer_list.next != &msg->transfers) {
  417. drv_data->cur_transfer =
  418. list_entry(trans->transfer_list.next,
  419. struct spi_transfer,
  420. transfer_list);
  421. return RUNNING_STATE;
  422. } else
  423. return DONE_STATE;
  424. }
  425. /* caller already set message->status; dma and pio irqs are blocked */
  426. static void giveback(struct driver_data *drv_data)
  427. {
  428. struct spi_transfer* last_transfer;
  429. struct spi_message *msg;
  430. msg = drv_data->cur_msg;
  431. drv_data->cur_msg = NULL;
  432. drv_data->cur_transfer = NULL;
  433. last_transfer = list_last_entry(&msg->transfers, struct spi_transfer,
  434. transfer_list);
  435. /* Delay if requested before any change in chip select */
  436. if (last_transfer->delay_usecs)
  437. udelay(last_transfer->delay_usecs);
  438. /* Drop chip select UNLESS cs_change is true or we are returning
  439. * a message with an error, or next message is for another chip
  440. */
  441. if (!last_transfer->cs_change)
  442. cs_deassert(drv_data);
  443. else {
  444. struct spi_message *next_msg;
  445. /* Holding of cs was hinted, but we need to make sure
  446. * the next message is for the same chip. Don't waste
  447. * time with the following tests unless this was hinted.
  448. *
  449. * We cannot postpone this until pump_messages, because
  450. * after calling msg->complete (below) the driver that
  451. * sent the current message could be unloaded, which
  452. * could invalidate the cs_control() callback...
  453. */
  454. /* get a pointer to the next message, if any */
  455. next_msg = spi_get_next_queued_message(drv_data->master);
  456. /* see if the next and current messages point
  457. * to the same chip
  458. */
  459. if (next_msg && next_msg->spi != msg->spi)
  460. next_msg = NULL;
  461. if (!next_msg || msg->state == ERROR_STATE)
  462. cs_deassert(drv_data);
  463. }
  464. drv_data->cur_chip = NULL;
  465. spi_finalize_current_message(drv_data->master);
  466. }
  467. static void reset_sccr1(struct driver_data *drv_data)
  468. {
  469. struct chip_data *chip = drv_data->cur_chip;
  470. u32 sccr1_reg;
  471. sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1) & ~drv_data->int_cr1;
  472. switch (drv_data->ssp_type) {
  473. case QUARK_X1000_SSP:
  474. sccr1_reg &= ~QUARK_X1000_SSCR1_RFT;
  475. break;
  476. default:
  477. sccr1_reg &= ~SSCR1_RFT;
  478. break;
  479. }
  480. sccr1_reg |= chip->threshold;
  481. pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
  482. }
  483. static void int_error_stop(struct driver_data *drv_data, const char* msg)
  484. {
  485. /* Stop and reset SSP */
  486. write_SSSR_CS(drv_data, drv_data->clear_sr);
  487. reset_sccr1(drv_data);
  488. if (!pxa25x_ssp_comp(drv_data))
  489. pxa2xx_spi_write(drv_data, SSTO, 0);
  490. pxa2xx_spi_flush(drv_data);
  491. pxa2xx_spi_write(drv_data, SSCR0,
  492. pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
  493. dev_err(&drv_data->pdev->dev, "%s\n", msg);
  494. drv_data->cur_msg->state = ERROR_STATE;
  495. tasklet_schedule(&drv_data->pump_transfers);
  496. }
  497. static void int_transfer_complete(struct driver_data *drv_data)
  498. {
  499. /* Stop SSP */
  500. write_SSSR_CS(drv_data, drv_data->clear_sr);
  501. reset_sccr1(drv_data);
  502. if (!pxa25x_ssp_comp(drv_data))
  503. pxa2xx_spi_write(drv_data, SSTO, 0);
  504. /* Update total byte transferred return count actual bytes read */
  505. drv_data->cur_msg->actual_length += drv_data->len -
  506. (drv_data->rx_end - drv_data->rx);
  507. /* Transfer delays and chip select release are
  508. * handled in pump_transfers or giveback
  509. */
  510. /* Move to next transfer */
  511. drv_data->cur_msg->state = pxa2xx_spi_next_transfer(drv_data);
  512. /* Schedule transfer tasklet */
  513. tasklet_schedule(&drv_data->pump_transfers);
  514. }
  515. static irqreturn_t interrupt_transfer(struct driver_data *drv_data)
  516. {
  517. u32 irq_mask = (pxa2xx_spi_read(drv_data, SSCR1) & SSCR1_TIE) ?
  518. drv_data->mask_sr : drv_data->mask_sr & ~SSSR_TFS;
  519. u32 irq_status = pxa2xx_spi_read(drv_data, SSSR) & irq_mask;
  520. if (irq_status & SSSR_ROR) {
  521. int_error_stop(drv_data, "interrupt_transfer: fifo overrun");
  522. return IRQ_HANDLED;
  523. }
  524. if (irq_status & SSSR_TINT) {
  525. pxa2xx_spi_write(drv_data, SSSR, SSSR_TINT);
  526. if (drv_data->read(drv_data)) {
  527. int_transfer_complete(drv_data);
  528. return IRQ_HANDLED;
  529. }
  530. }
  531. /* Drain rx fifo, Fill tx fifo and prevent overruns */
  532. do {
  533. if (drv_data->read(drv_data)) {
  534. int_transfer_complete(drv_data);
  535. return IRQ_HANDLED;
  536. }
  537. } while (drv_data->write(drv_data));
  538. if (drv_data->read(drv_data)) {
  539. int_transfer_complete(drv_data);
  540. return IRQ_HANDLED;
  541. }
  542. if (drv_data->tx == drv_data->tx_end) {
  543. u32 bytes_left;
  544. u32 sccr1_reg;
  545. sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
  546. sccr1_reg &= ~SSCR1_TIE;
  547. /*
  548. * PXA25x_SSP has no timeout, set up rx threshould for the
  549. * remaining RX bytes.
  550. */
  551. if (pxa25x_ssp_comp(drv_data)) {
  552. u32 rx_thre;
  553. pxa2xx_spi_clear_rx_thre(drv_data, &sccr1_reg);
  554. bytes_left = drv_data->rx_end - drv_data->rx;
  555. switch (drv_data->n_bytes) {
  556. case 4:
  557. bytes_left >>= 1;
  558. case 2:
  559. bytes_left >>= 1;
  560. }
  561. rx_thre = pxa2xx_spi_get_rx_default_thre(drv_data);
  562. if (rx_thre > bytes_left)
  563. rx_thre = bytes_left;
  564. pxa2xx_spi_set_rx_thre(drv_data, &sccr1_reg, rx_thre);
  565. }
  566. pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
  567. }
  568. /* We did something */
  569. return IRQ_HANDLED;
  570. }
  571. static irqreturn_t ssp_int(int irq, void *dev_id)
  572. {
  573. struct driver_data *drv_data = dev_id;
  574. u32 sccr1_reg;
  575. u32 mask = drv_data->mask_sr;
  576. u32 status;
  577. /*
  578. * The IRQ might be shared with other peripherals so we must first
  579. * check that are we RPM suspended or not. If we are we assume that
  580. * the IRQ was not for us (we shouldn't be RPM suspended when the
  581. * interrupt is enabled).
  582. */
  583. if (pm_runtime_suspended(&drv_data->pdev->dev))
  584. return IRQ_NONE;
  585. /*
  586. * If the device is not yet in RPM suspended state and we get an
  587. * interrupt that is meant for another device, check if status bits
  588. * are all set to one. That means that the device is already
  589. * powered off.
  590. */
  591. status = pxa2xx_spi_read(drv_data, SSSR);
  592. if (status == ~0)
  593. return IRQ_NONE;
  594. sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
  595. /* Ignore possible writes if we don't need to write */
  596. if (!(sccr1_reg & SSCR1_TIE))
  597. mask &= ~SSSR_TFS;
  598. /* Ignore RX timeout interrupt if it is disabled */
  599. if (!(sccr1_reg & SSCR1_TINTE))
  600. mask &= ~SSSR_TINT;
  601. if (!(status & mask))
  602. return IRQ_NONE;
  603. if (!drv_data->cur_msg) {
  604. pxa2xx_spi_write(drv_data, SSCR0,
  605. pxa2xx_spi_read(drv_data, SSCR0)
  606. & ~SSCR0_SSE);
  607. pxa2xx_spi_write(drv_data, SSCR1,
  608. pxa2xx_spi_read(drv_data, SSCR1)
  609. & ~drv_data->int_cr1);
  610. if (!pxa25x_ssp_comp(drv_data))
  611. pxa2xx_spi_write(drv_data, SSTO, 0);
  612. write_SSSR_CS(drv_data, drv_data->clear_sr);
  613. dev_err(&drv_data->pdev->dev,
  614. "bad message state in interrupt handler\n");
  615. /* Never fail */
  616. return IRQ_HANDLED;
  617. }
  618. return drv_data->transfer_handler(drv_data);
  619. }
  620. /*
  621. * The Quark SPI has an additional 24 bit register (DDS_CLK_RATE) to multiply
  622. * input frequency by fractions of 2^24. It also has a divider by 5.
  623. *
  624. * There are formulas to get baud rate value for given input frequency and
  625. * divider parameters, such as DDS_CLK_RATE and SCR:
  626. *
  627. * Fsys = 200MHz
  628. *
  629. * Fssp = Fsys * DDS_CLK_RATE / 2^24 (1)
  630. * Baud rate = Fsclk = Fssp / (2 * (SCR + 1)) (2)
  631. *
  632. * DDS_CLK_RATE either 2^n or 2^n / 5.
  633. * SCR is in range 0 .. 255
  634. *
  635. * Divisor = 5^i * 2^j * 2 * k
  636. * i = [0, 1] i = 1 iff j = 0 or j > 3
  637. * j = [0, 23] j = 0 iff i = 1
  638. * k = [1, 256]
  639. * Special case: j = 0, i = 1: Divisor = 2 / 5
  640. *
  641. * Accordingly to the specification the recommended values for DDS_CLK_RATE
  642. * are:
  643. * Case 1: 2^n, n = [0, 23]
  644. * Case 2: 2^24 * 2 / 5 (0x666666)
  645. * Case 3: less than or equal to 2^24 / 5 / 16 (0x33333)
  646. *
  647. * In all cases the lowest possible value is better.
  648. *
  649. * The function calculates parameters for all cases and chooses the one closest
  650. * to the asked baud rate.
  651. */
  652. static unsigned int quark_x1000_get_clk_div(int rate, u32 *dds)
  653. {
  654. unsigned long xtal = 200000000;
  655. unsigned long fref = xtal / 2; /* mandatory division by 2,
  656. see (2) */
  657. /* case 3 */
  658. unsigned long fref1 = fref / 2; /* case 1 */
  659. unsigned long fref2 = fref * 2 / 5; /* case 2 */
  660. unsigned long scale;
  661. unsigned long q, q1, q2;
  662. long r, r1, r2;
  663. u32 mul;
  664. /* Case 1 */
  665. /* Set initial value for DDS_CLK_RATE */
  666. mul = (1 << 24) >> 1;
  667. /* Calculate initial quot */
  668. q1 = DIV_ROUND_UP(fref1, rate);
  669. /* Scale q1 if it's too big */
  670. if (q1 > 256) {
  671. /* Scale q1 to range [1, 512] */
  672. scale = fls_long(q1 - 1);
  673. if (scale > 9) {
  674. q1 >>= scale - 9;
  675. mul >>= scale - 9;
  676. }
  677. /* Round the result if we have a remainder */
  678. q1 += q1 & 1;
  679. }
  680. /* Decrease DDS_CLK_RATE as much as we can without loss in precision */
  681. scale = __ffs(q1);
  682. q1 >>= scale;
  683. mul >>= scale;
  684. /* Get the remainder */
  685. r1 = abs(fref1 / (1 << (24 - fls_long(mul))) / q1 - rate);
  686. /* Case 2 */
  687. q2 = DIV_ROUND_UP(fref2, rate);
  688. r2 = abs(fref2 / q2 - rate);
  689. /*
  690. * Choose the best between two: less remainder we have the better. We
  691. * can't go case 2 if q2 is greater than 256 since SCR register can
  692. * hold only values 0 .. 255.
  693. */
  694. if (r2 >= r1 || q2 > 256) {
  695. /* case 1 is better */
  696. r = r1;
  697. q = q1;
  698. } else {
  699. /* case 2 is better */
  700. r = r2;
  701. q = q2;
  702. mul = (1 << 24) * 2 / 5;
  703. }
  704. /* Check case 3 only if the divisor is big enough */
  705. if (fref / rate >= 80) {
  706. u64 fssp;
  707. u32 m;
  708. /* Calculate initial quot */
  709. q1 = DIV_ROUND_UP(fref, rate);
  710. m = (1 << 24) / q1;
  711. /* Get the remainder */
  712. fssp = (u64)fref * m;
  713. do_div(fssp, 1 << 24);
  714. r1 = abs(fssp - rate);
  715. /* Choose this one if it suits better */
  716. if (r1 < r) {
  717. /* case 3 is better */
  718. q = 1;
  719. mul = m;
  720. }
  721. }
  722. *dds = mul;
  723. return q - 1;
  724. }
  725. static unsigned int ssp_get_clk_div(struct driver_data *drv_data, int rate)
  726. {
  727. unsigned long ssp_clk = drv_data->master->max_speed_hz;
  728. const struct ssp_device *ssp = drv_data->ssp;
  729. rate = min_t(int, ssp_clk, rate);
  730. if (ssp->type == PXA25x_SSP || ssp->type == CE4100_SSP)
  731. return (ssp_clk / (2 * rate) - 1) & 0xff;
  732. else
  733. return (ssp_clk / rate - 1) & 0xfff;
  734. }
  735. static unsigned int pxa2xx_ssp_get_clk_div(struct driver_data *drv_data,
  736. int rate)
  737. {
  738. struct chip_data *chip = drv_data->cur_chip;
  739. unsigned int clk_div;
  740. switch (drv_data->ssp_type) {
  741. case QUARK_X1000_SSP:
  742. clk_div = quark_x1000_get_clk_div(rate, &chip->dds_rate);
  743. break;
  744. default:
  745. clk_div = ssp_get_clk_div(drv_data, rate);
  746. break;
  747. }
  748. return clk_div << 8;
  749. }
  750. static void pump_transfers(unsigned long data)
  751. {
  752. struct driver_data *drv_data = (struct driver_data *)data;
  753. struct spi_message *message = NULL;
  754. struct spi_transfer *transfer = NULL;
  755. struct spi_transfer *previous = NULL;
  756. struct chip_data *chip = NULL;
  757. u32 clk_div = 0;
  758. u8 bits = 0;
  759. u32 speed = 0;
  760. u32 cr0;
  761. u32 cr1;
  762. u32 dma_thresh = drv_data->cur_chip->dma_threshold;
  763. u32 dma_burst = drv_data->cur_chip->dma_burst_size;
  764. u32 change_mask = pxa2xx_spi_get_ssrc1_change_mask(drv_data);
  765. /* Get current state information */
  766. message = drv_data->cur_msg;
  767. transfer = drv_data->cur_transfer;
  768. chip = drv_data->cur_chip;
  769. /* Handle for abort */
  770. if (message->state == ERROR_STATE) {
  771. message->status = -EIO;
  772. giveback(drv_data);
  773. return;
  774. }
  775. /* Handle end of message */
  776. if (message->state == DONE_STATE) {
  777. message->status = 0;
  778. giveback(drv_data);
  779. return;
  780. }
  781. /* Delay if requested at end of transfer before CS change */
  782. if (message->state == RUNNING_STATE) {
  783. previous = list_entry(transfer->transfer_list.prev,
  784. struct spi_transfer,
  785. transfer_list);
  786. if (previous->delay_usecs)
  787. udelay(previous->delay_usecs);
  788. /* Drop chip select only if cs_change is requested */
  789. if (previous->cs_change)
  790. cs_deassert(drv_data);
  791. }
  792. /* Check if we can DMA this transfer */
  793. if (!pxa2xx_spi_dma_is_possible(transfer->len) && chip->enable_dma) {
  794. /* reject already-mapped transfers; PIO won't always work */
  795. if (message->is_dma_mapped
  796. || transfer->rx_dma || transfer->tx_dma) {
  797. dev_err(&drv_data->pdev->dev,
  798. "pump_transfers: mapped transfer length of "
  799. "%u is greater than %d\n",
  800. transfer->len, MAX_DMA_LEN);
  801. message->status = -EINVAL;
  802. giveback(drv_data);
  803. return;
  804. }
  805. /* warn ... we force this to PIO mode */
  806. dev_warn_ratelimited(&message->spi->dev,
  807. "pump_transfers: DMA disabled for transfer length %ld "
  808. "greater than %d\n",
  809. (long)drv_data->len, MAX_DMA_LEN);
  810. }
  811. /* Setup the transfer state based on the type of transfer */
  812. if (pxa2xx_spi_flush(drv_data) == 0) {
  813. dev_err(&drv_data->pdev->dev, "pump_transfers: flush failed\n");
  814. message->status = -EIO;
  815. giveback(drv_data);
  816. return;
  817. }
  818. drv_data->n_bytes = chip->n_bytes;
  819. drv_data->tx = (void *)transfer->tx_buf;
  820. drv_data->tx_end = drv_data->tx + transfer->len;
  821. drv_data->rx = transfer->rx_buf;
  822. drv_data->rx_end = drv_data->rx + transfer->len;
  823. drv_data->rx_dma = transfer->rx_dma;
  824. drv_data->tx_dma = transfer->tx_dma;
  825. drv_data->len = transfer->len;
  826. drv_data->write = drv_data->tx ? chip->write : null_writer;
  827. drv_data->read = drv_data->rx ? chip->read : null_reader;
  828. /* Change speed and bit per word on a per transfer */
  829. bits = transfer->bits_per_word;
  830. speed = transfer->speed_hz;
  831. clk_div = pxa2xx_ssp_get_clk_div(drv_data, speed);
  832. if (bits <= 8) {
  833. drv_data->n_bytes = 1;
  834. drv_data->read = drv_data->read != null_reader ?
  835. u8_reader : null_reader;
  836. drv_data->write = drv_data->write != null_writer ?
  837. u8_writer : null_writer;
  838. } else if (bits <= 16) {
  839. drv_data->n_bytes = 2;
  840. drv_data->read = drv_data->read != null_reader ?
  841. u16_reader : null_reader;
  842. drv_data->write = drv_data->write != null_writer ?
  843. u16_writer : null_writer;
  844. } else if (bits <= 32) {
  845. drv_data->n_bytes = 4;
  846. drv_data->read = drv_data->read != null_reader ?
  847. u32_reader : null_reader;
  848. drv_data->write = drv_data->write != null_writer ?
  849. u32_writer : null_writer;
  850. }
  851. /*
  852. * if bits/word is changed in dma mode, then must check the
  853. * thresholds and burst also
  854. */
  855. if (chip->enable_dma) {
  856. if (pxa2xx_spi_set_dma_burst_and_threshold(chip,
  857. message->spi,
  858. bits, &dma_burst,
  859. &dma_thresh))
  860. dev_warn_ratelimited(&message->spi->dev,
  861. "pump_transfers: DMA burst size reduced to match bits_per_word\n");
  862. }
  863. /* NOTE: PXA25x_SSP _could_ use external clocking ... */
  864. cr0 = pxa2xx_configure_sscr0(drv_data, clk_div, bits);
  865. if (!pxa25x_ssp_comp(drv_data))
  866. dev_dbg(&message->spi->dev, "%u Hz actual, %s\n",
  867. drv_data->master->max_speed_hz
  868. / (1 + ((cr0 & SSCR0_SCR(0xfff)) >> 8)),
  869. chip->enable_dma ? "DMA" : "PIO");
  870. else
  871. dev_dbg(&message->spi->dev, "%u Hz actual, %s\n",
  872. drv_data->master->max_speed_hz / 2
  873. / (1 + ((cr0 & SSCR0_SCR(0x0ff)) >> 8)),
  874. chip->enable_dma ? "DMA" : "PIO");
  875. message->state = RUNNING_STATE;
  876. drv_data->dma_mapped = 0;
  877. if (pxa2xx_spi_dma_is_possible(drv_data->len))
  878. drv_data->dma_mapped = pxa2xx_spi_map_dma_buffers(drv_data);
  879. if (drv_data->dma_mapped) {
  880. /* Ensure we have the correct interrupt handler */
  881. drv_data->transfer_handler = pxa2xx_spi_dma_transfer;
  882. pxa2xx_spi_dma_prepare(drv_data, dma_burst);
  883. /* Clear status and start DMA engine */
  884. cr1 = chip->cr1 | dma_thresh | drv_data->dma_cr1;
  885. pxa2xx_spi_write(drv_data, SSSR, drv_data->clear_sr);
  886. pxa2xx_spi_dma_start(drv_data);
  887. } else {
  888. /* Ensure we have the correct interrupt handler */
  889. drv_data->transfer_handler = interrupt_transfer;
  890. /* Clear status */
  891. cr1 = chip->cr1 | chip->threshold | drv_data->int_cr1;
  892. write_SSSR_CS(drv_data, drv_data->clear_sr);
  893. }
  894. if (is_lpss_ssp(drv_data)) {
  895. if ((pxa2xx_spi_read(drv_data, SSIRF) & 0xff)
  896. != chip->lpss_rx_threshold)
  897. pxa2xx_spi_write(drv_data, SSIRF,
  898. chip->lpss_rx_threshold);
  899. if ((pxa2xx_spi_read(drv_data, SSITF) & 0xffff)
  900. != chip->lpss_tx_threshold)
  901. pxa2xx_spi_write(drv_data, SSITF,
  902. chip->lpss_tx_threshold);
  903. }
  904. if (is_quark_x1000_ssp(drv_data) &&
  905. (pxa2xx_spi_read(drv_data, DDS_RATE) != chip->dds_rate))
  906. pxa2xx_spi_write(drv_data, DDS_RATE, chip->dds_rate);
  907. /* see if we need to reload the config registers */
  908. if ((pxa2xx_spi_read(drv_data, SSCR0) != cr0)
  909. || (pxa2xx_spi_read(drv_data, SSCR1) & change_mask)
  910. != (cr1 & change_mask)) {
  911. /* stop the SSP, and update the other bits */
  912. pxa2xx_spi_write(drv_data, SSCR0, cr0 & ~SSCR0_SSE);
  913. if (!pxa25x_ssp_comp(drv_data))
  914. pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
  915. /* first set CR1 without interrupt and service enables */
  916. pxa2xx_spi_write(drv_data, SSCR1, cr1 & change_mask);
  917. /* restart the SSP */
  918. pxa2xx_spi_write(drv_data, SSCR0, cr0);
  919. } else {
  920. if (!pxa25x_ssp_comp(drv_data))
  921. pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
  922. }
  923. cs_assert(drv_data);
  924. /* after chip select, release the data by enabling service
  925. * requests and interrupts, without changing any mode bits */
  926. pxa2xx_spi_write(drv_data, SSCR1, cr1);
  927. }
  928. static int pxa2xx_spi_transfer_one_message(struct spi_master *master,
  929. struct spi_message *msg)
  930. {
  931. struct driver_data *drv_data = spi_master_get_devdata(master);
  932. drv_data->cur_msg = msg;
  933. /* Initial message state*/
  934. drv_data->cur_msg->state = START_STATE;
  935. drv_data->cur_transfer = list_entry(drv_data->cur_msg->transfers.next,
  936. struct spi_transfer,
  937. transfer_list);
  938. /* prepare to setup the SSP, in pump_transfers, using the per
  939. * chip configuration */
  940. drv_data->cur_chip = spi_get_ctldata(drv_data->cur_msg->spi);
  941. /* Mark as busy and launch transfers */
  942. tasklet_schedule(&drv_data->pump_transfers);
  943. return 0;
  944. }
  945. static int pxa2xx_spi_unprepare_transfer(struct spi_master *master)
  946. {
  947. struct driver_data *drv_data = spi_master_get_devdata(master);
  948. /* Disable the SSP now */
  949. pxa2xx_spi_write(drv_data, SSCR0,
  950. pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
  951. return 0;
  952. }
  953. static int setup_cs(struct spi_device *spi, struct chip_data *chip,
  954. struct pxa2xx_spi_chip *chip_info)
  955. {
  956. int err = 0;
  957. if (chip == NULL || chip_info == NULL)
  958. return 0;
  959. /* NOTE: setup() can be called multiple times, possibly with
  960. * different chip_info, release previously requested GPIO
  961. */
  962. if (gpio_is_valid(chip->gpio_cs))
  963. gpio_free(chip->gpio_cs);
  964. /* If (*cs_control) is provided, ignore GPIO chip select */
  965. if (chip_info->cs_control) {
  966. chip->cs_control = chip_info->cs_control;
  967. return 0;
  968. }
  969. if (gpio_is_valid(chip_info->gpio_cs)) {
  970. err = gpio_request(chip_info->gpio_cs, "SPI_CS");
  971. if (err) {
  972. dev_err(&spi->dev, "failed to request chip select GPIO%d\n",
  973. chip_info->gpio_cs);
  974. return err;
  975. }
  976. chip->gpio_cs = chip_info->gpio_cs;
  977. chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH;
  978. err = gpio_direction_output(chip->gpio_cs,
  979. !chip->gpio_cs_inverted);
  980. }
  981. return err;
  982. }
  983. static int setup(struct spi_device *spi)
  984. {
  985. struct pxa2xx_spi_chip *chip_info = NULL;
  986. struct chip_data *chip;
  987. const struct lpss_config *config;
  988. struct driver_data *drv_data = spi_master_get_devdata(spi->master);
  989. uint tx_thres, tx_hi_thres, rx_thres;
  990. switch (drv_data->ssp_type) {
  991. case QUARK_X1000_SSP:
  992. tx_thres = TX_THRESH_QUARK_X1000_DFLT;
  993. tx_hi_thres = 0;
  994. rx_thres = RX_THRESH_QUARK_X1000_DFLT;
  995. break;
  996. case LPSS_LPT_SSP:
  997. case LPSS_BYT_SSP:
  998. case LPSS_SPT_SSP:
  999. case LPSS_BXT_SSP:
  1000. config = lpss_get_config(drv_data);
  1001. tx_thres = config->tx_threshold_lo;
  1002. tx_hi_thres = config->tx_threshold_hi;
  1003. rx_thres = config->rx_threshold;
  1004. break;
  1005. default:
  1006. tx_thres = TX_THRESH_DFLT;
  1007. tx_hi_thres = 0;
  1008. rx_thres = RX_THRESH_DFLT;
  1009. break;
  1010. }
  1011. /* Only alloc on first setup */
  1012. chip = spi_get_ctldata(spi);
  1013. if (!chip) {
  1014. chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
  1015. if (!chip)
  1016. return -ENOMEM;
  1017. if (drv_data->ssp_type == CE4100_SSP) {
  1018. if (spi->chip_select > 4) {
  1019. dev_err(&spi->dev,
  1020. "failed setup: cs number must not be > 4.\n");
  1021. kfree(chip);
  1022. return -EINVAL;
  1023. }
  1024. chip->frm = spi->chip_select;
  1025. } else
  1026. chip->gpio_cs = -1;
  1027. chip->enable_dma = 0;
  1028. chip->timeout = TIMOUT_DFLT;
  1029. }
  1030. /* protocol drivers may change the chip settings, so...
  1031. * if chip_info exists, use it */
  1032. chip_info = spi->controller_data;
  1033. /* chip_info isn't always needed */
  1034. chip->cr1 = 0;
  1035. if (chip_info) {
  1036. if (chip_info->timeout)
  1037. chip->timeout = chip_info->timeout;
  1038. if (chip_info->tx_threshold)
  1039. tx_thres = chip_info->tx_threshold;
  1040. if (chip_info->tx_hi_threshold)
  1041. tx_hi_thres = chip_info->tx_hi_threshold;
  1042. if (chip_info->rx_threshold)
  1043. rx_thres = chip_info->rx_threshold;
  1044. chip->enable_dma = drv_data->master_info->enable_dma;
  1045. chip->dma_threshold = 0;
  1046. if (chip_info->enable_loopback)
  1047. chip->cr1 = SSCR1_LBM;
  1048. } else if (ACPI_HANDLE(&spi->dev)) {
  1049. /*
  1050. * Slave devices enumerated from ACPI namespace don't
  1051. * usually have chip_info but we still might want to use
  1052. * DMA with them.
  1053. */
  1054. chip->enable_dma = drv_data->master_info->enable_dma;
  1055. }
  1056. chip->lpss_rx_threshold = SSIRF_RxThresh(rx_thres);
  1057. chip->lpss_tx_threshold = SSITF_TxLoThresh(tx_thres)
  1058. | SSITF_TxHiThresh(tx_hi_thres);
  1059. /* set dma burst and threshold outside of chip_info path so that if
  1060. * chip_info goes away after setting chip->enable_dma, the
  1061. * burst and threshold can still respond to changes in bits_per_word */
  1062. if (chip->enable_dma) {
  1063. /* set up legal burst and threshold for dma */
  1064. if (pxa2xx_spi_set_dma_burst_and_threshold(chip, spi,
  1065. spi->bits_per_word,
  1066. &chip->dma_burst_size,
  1067. &chip->dma_threshold)) {
  1068. dev_warn(&spi->dev,
  1069. "in setup: DMA burst size reduced to match bits_per_word\n");
  1070. }
  1071. }
  1072. switch (drv_data->ssp_type) {
  1073. case QUARK_X1000_SSP:
  1074. chip->threshold = (QUARK_X1000_SSCR1_RxTresh(rx_thres)
  1075. & QUARK_X1000_SSCR1_RFT)
  1076. | (QUARK_X1000_SSCR1_TxTresh(tx_thres)
  1077. & QUARK_X1000_SSCR1_TFT);
  1078. break;
  1079. default:
  1080. chip->threshold = (SSCR1_RxTresh(rx_thres) & SSCR1_RFT) |
  1081. (SSCR1_TxTresh(tx_thres) & SSCR1_TFT);
  1082. break;
  1083. }
  1084. chip->cr1 &= ~(SSCR1_SPO | SSCR1_SPH);
  1085. chip->cr1 |= (((spi->mode & SPI_CPHA) != 0) ? SSCR1_SPH : 0)
  1086. | (((spi->mode & SPI_CPOL) != 0) ? SSCR1_SPO : 0);
  1087. if (spi->mode & SPI_LOOP)
  1088. chip->cr1 |= SSCR1_LBM;
  1089. if (spi->bits_per_word <= 8) {
  1090. chip->n_bytes = 1;
  1091. chip->read = u8_reader;
  1092. chip->write = u8_writer;
  1093. } else if (spi->bits_per_word <= 16) {
  1094. chip->n_bytes = 2;
  1095. chip->read = u16_reader;
  1096. chip->write = u16_writer;
  1097. } else if (spi->bits_per_word <= 32) {
  1098. chip->n_bytes = 4;
  1099. chip->read = u32_reader;
  1100. chip->write = u32_writer;
  1101. }
  1102. spi_set_ctldata(spi, chip);
  1103. if (drv_data->ssp_type == CE4100_SSP)
  1104. return 0;
  1105. return setup_cs(spi, chip, chip_info);
  1106. }
  1107. static void cleanup(struct spi_device *spi)
  1108. {
  1109. struct chip_data *chip = spi_get_ctldata(spi);
  1110. struct driver_data *drv_data = spi_master_get_devdata(spi->master);
  1111. if (!chip)
  1112. return;
  1113. if (drv_data->ssp_type != CE4100_SSP && gpio_is_valid(chip->gpio_cs))
  1114. gpio_free(chip->gpio_cs);
  1115. kfree(chip);
  1116. }
  1117. #ifdef CONFIG_PCI
  1118. #ifdef CONFIG_ACPI
  1119. static const struct acpi_device_id pxa2xx_spi_acpi_match[] = {
  1120. { "INT33C0", LPSS_LPT_SSP },
  1121. { "INT33C1", LPSS_LPT_SSP },
  1122. { "INT3430", LPSS_LPT_SSP },
  1123. { "INT3431", LPSS_LPT_SSP },
  1124. { "80860F0E", LPSS_BYT_SSP },
  1125. { "8086228E", LPSS_BYT_SSP },
  1126. { },
  1127. };
  1128. MODULE_DEVICE_TABLE(acpi, pxa2xx_spi_acpi_match);
  1129. static int pxa2xx_spi_get_port_id(struct acpi_device *adev)
  1130. {
  1131. unsigned int devid;
  1132. int port_id = -1;
  1133. if (adev && adev->pnp.unique_id &&
  1134. !kstrtouint(adev->pnp.unique_id, 0, &devid))
  1135. port_id = devid;
  1136. return port_id;
  1137. }
  1138. #else /* !CONFIG_ACPI */
  1139. static int pxa2xx_spi_get_port_id(struct acpi_device *adev)
  1140. {
  1141. return -1;
  1142. }
  1143. #endif
  1144. /*
  1145. * PCI IDs of compound devices that integrate both host controller and private
  1146. * integrated DMA engine. Please note these are not used in module
  1147. * autoloading and probing in this module but matching the LPSS SSP type.
  1148. */
  1149. static const struct pci_device_id pxa2xx_spi_pci_compound_match[] = {
  1150. /* SPT-LP */
  1151. { PCI_VDEVICE(INTEL, 0x9d29), LPSS_SPT_SSP },
  1152. { PCI_VDEVICE(INTEL, 0x9d2a), LPSS_SPT_SSP },
  1153. /* SPT-H */
  1154. { PCI_VDEVICE(INTEL, 0xa129), LPSS_SPT_SSP },
  1155. { PCI_VDEVICE(INTEL, 0xa12a), LPSS_SPT_SSP },
  1156. /* BXT */
  1157. { PCI_VDEVICE(INTEL, 0x0ac2), LPSS_BXT_SSP },
  1158. { PCI_VDEVICE(INTEL, 0x0ac4), LPSS_BXT_SSP },
  1159. { PCI_VDEVICE(INTEL, 0x0ac6), LPSS_BXT_SSP },
  1160. /* APL */
  1161. { PCI_VDEVICE(INTEL, 0x5ac2), LPSS_BXT_SSP },
  1162. { PCI_VDEVICE(INTEL, 0x5ac4), LPSS_BXT_SSP },
  1163. { PCI_VDEVICE(INTEL, 0x5ac6), LPSS_BXT_SSP },
  1164. { },
  1165. };
  1166. static bool pxa2xx_spi_idma_filter(struct dma_chan *chan, void *param)
  1167. {
  1168. struct device *dev = param;
  1169. if (dev != chan->device->dev->parent)
  1170. return false;
  1171. return true;
  1172. }
  1173. static struct pxa2xx_spi_master *
  1174. pxa2xx_spi_init_pdata(struct platform_device *pdev)
  1175. {
  1176. struct pxa2xx_spi_master *pdata;
  1177. struct acpi_device *adev;
  1178. struct ssp_device *ssp;
  1179. struct resource *res;
  1180. const struct acpi_device_id *adev_id = NULL;
  1181. const struct pci_device_id *pcidev_id = NULL;
  1182. int type;
  1183. adev = ACPI_COMPANION(&pdev->dev);
  1184. if (dev_is_pci(pdev->dev.parent))
  1185. pcidev_id = pci_match_id(pxa2xx_spi_pci_compound_match,
  1186. to_pci_dev(pdev->dev.parent));
  1187. else if (adev)
  1188. adev_id = acpi_match_device(pdev->dev.driver->acpi_match_table,
  1189. &pdev->dev);
  1190. else
  1191. return NULL;
  1192. if (adev_id)
  1193. type = (int)adev_id->driver_data;
  1194. else if (pcidev_id)
  1195. type = (int)pcidev_id->driver_data;
  1196. else
  1197. return NULL;
  1198. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  1199. if (!pdata)
  1200. return NULL;
  1201. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1202. if (!res)
  1203. return NULL;
  1204. ssp = &pdata->ssp;
  1205. ssp->phys_base = res->start;
  1206. ssp->mmio_base = devm_ioremap_resource(&pdev->dev, res);
  1207. if (IS_ERR(ssp->mmio_base))
  1208. return NULL;
  1209. if (pcidev_id) {
  1210. pdata->tx_param = pdev->dev.parent;
  1211. pdata->rx_param = pdev->dev.parent;
  1212. pdata->dma_filter = pxa2xx_spi_idma_filter;
  1213. }
  1214. ssp->clk = devm_clk_get(&pdev->dev, NULL);
  1215. ssp->irq = platform_get_irq(pdev, 0);
  1216. ssp->type = type;
  1217. ssp->pdev = pdev;
  1218. ssp->port_id = pxa2xx_spi_get_port_id(adev);
  1219. pdata->num_chipselect = 1;
  1220. pdata->enable_dma = true;
  1221. return pdata;
  1222. }
  1223. #else /* !CONFIG_PCI */
  1224. static inline struct pxa2xx_spi_master *
  1225. pxa2xx_spi_init_pdata(struct platform_device *pdev)
  1226. {
  1227. return NULL;
  1228. }
  1229. #endif
  1230. static int pxa2xx_spi_probe(struct platform_device *pdev)
  1231. {
  1232. struct device *dev = &pdev->dev;
  1233. struct pxa2xx_spi_master *platform_info;
  1234. struct spi_master *master;
  1235. struct driver_data *drv_data;
  1236. struct ssp_device *ssp;
  1237. const struct lpss_config *config;
  1238. int status;
  1239. u32 tmp;
  1240. platform_info = dev_get_platdata(dev);
  1241. if (!platform_info) {
  1242. platform_info = pxa2xx_spi_init_pdata(pdev);
  1243. if (!platform_info) {
  1244. dev_err(&pdev->dev, "missing platform data\n");
  1245. return -ENODEV;
  1246. }
  1247. }
  1248. ssp = pxa_ssp_request(pdev->id, pdev->name);
  1249. if (!ssp)
  1250. ssp = &platform_info->ssp;
  1251. if (!ssp->mmio_base) {
  1252. dev_err(&pdev->dev, "failed to get ssp\n");
  1253. return -ENODEV;
  1254. }
  1255. master = spi_alloc_master(dev, sizeof(struct driver_data));
  1256. if (!master) {
  1257. dev_err(&pdev->dev, "cannot alloc spi_master\n");
  1258. pxa_ssp_free(ssp);
  1259. return -ENOMEM;
  1260. }
  1261. drv_data = spi_master_get_devdata(master);
  1262. drv_data->master = master;
  1263. drv_data->master_info = platform_info;
  1264. drv_data->pdev = pdev;
  1265. drv_data->ssp = ssp;
  1266. master->dev.parent = &pdev->dev;
  1267. master->dev.of_node = pdev->dev.of_node;
  1268. /* the spi->mode bits understood by this driver: */
  1269. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP;
  1270. master->bus_num = ssp->port_id;
  1271. master->dma_alignment = DMA_ALIGNMENT;
  1272. master->cleanup = cleanup;
  1273. master->setup = setup;
  1274. master->transfer_one_message = pxa2xx_spi_transfer_one_message;
  1275. master->unprepare_transfer_hardware = pxa2xx_spi_unprepare_transfer;
  1276. master->auto_runtime_pm = true;
  1277. drv_data->ssp_type = ssp->type;
  1278. drv_data->ioaddr = ssp->mmio_base;
  1279. drv_data->ssdr_physical = ssp->phys_base + SSDR;
  1280. if (pxa25x_ssp_comp(drv_data)) {
  1281. switch (drv_data->ssp_type) {
  1282. case QUARK_X1000_SSP:
  1283. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
  1284. break;
  1285. default:
  1286. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
  1287. break;
  1288. }
  1289. drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE;
  1290. drv_data->dma_cr1 = 0;
  1291. drv_data->clear_sr = SSSR_ROR;
  1292. drv_data->mask_sr = SSSR_RFS | SSSR_TFS | SSSR_ROR;
  1293. } else {
  1294. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
  1295. drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE;
  1296. drv_data->dma_cr1 = DEFAULT_DMA_CR1;
  1297. drv_data->clear_sr = SSSR_ROR | SSSR_TINT;
  1298. drv_data->mask_sr = SSSR_TINT | SSSR_RFS | SSSR_TFS | SSSR_ROR;
  1299. }
  1300. status = request_irq(ssp->irq, ssp_int, IRQF_SHARED, dev_name(dev),
  1301. drv_data);
  1302. if (status < 0) {
  1303. dev_err(&pdev->dev, "cannot get IRQ %d\n", ssp->irq);
  1304. goto out_error_master_alloc;
  1305. }
  1306. /* Setup DMA if requested */
  1307. if (platform_info->enable_dma) {
  1308. status = pxa2xx_spi_dma_setup(drv_data);
  1309. if (status) {
  1310. dev_dbg(dev, "no DMA channels available, using PIO\n");
  1311. platform_info->enable_dma = false;
  1312. }
  1313. }
  1314. /* Enable SOC clock */
  1315. clk_prepare_enable(ssp->clk);
  1316. master->max_speed_hz = clk_get_rate(ssp->clk);
  1317. /* Load default SSP configuration */
  1318. pxa2xx_spi_write(drv_data, SSCR0, 0);
  1319. switch (drv_data->ssp_type) {
  1320. case QUARK_X1000_SSP:
  1321. tmp = QUARK_X1000_SSCR1_RxTresh(RX_THRESH_QUARK_X1000_DFLT)
  1322. | QUARK_X1000_SSCR1_TxTresh(TX_THRESH_QUARK_X1000_DFLT);
  1323. pxa2xx_spi_write(drv_data, SSCR1, tmp);
  1324. /* using the Motorola SPI protocol and use 8 bit frame */
  1325. pxa2xx_spi_write(drv_data, SSCR0,
  1326. QUARK_X1000_SSCR0_Motorola
  1327. | QUARK_X1000_SSCR0_DataSize(8));
  1328. break;
  1329. default:
  1330. tmp = SSCR1_RxTresh(RX_THRESH_DFLT) |
  1331. SSCR1_TxTresh(TX_THRESH_DFLT);
  1332. pxa2xx_spi_write(drv_data, SSCR1, tmp);
  1333. tmp = SSCR0_SCR(2) | SSCR0_Motorola | SSCR0_DataSize(8);
  1334. pxa2xx_spi_write(drv_data, SSCR0, tmp);
  1335. break;
  1336. }
  1337. if (!pxa25x_ssp_comp(drv_data))
  1338. pxa2xx_spi_write(drv_data, SSTO, 0);
  1339. if (!is_quark_x1000_ssp(drv_data))
  1340. pxa2xx_spi_write(drv_data, SSPSP, 0);
  1341. if (is_lpss_ssp(drv_data))
  1342. lpss_ssp_setup(drv_data);
  1343. if (is_lpss_ssp(drv_data)) {
  1344. lpss_ssp_setup(drv_data);
  1345. config = lpss_get_config(drv_data);
  1346. if (config->reg_capabilities >= 0) {
  1347. tmp = __lpss_ssp_read_priv(drv_data,
  1348. config->reg_capabilities);
  1349. tmp &= LPSS_CAPS_CS_EN_MASK;
  1350. tmp >>= LPSS_CAPS_CS_EN_SHIFT;
  1351. platform_info->num_chipselect = ffz(tmp);
  1352. }
  1353. }
  1354. master->num_chipselect = platform_info->num_chipselect;
  1355. tasklet_init(&drv_data->pump_transfers, pump_transfers,
  1356. (unsigned long)drv_data);
  1357. pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
  1358. pm_runtime_use_autosuspend(&pdev->dev);
  1359. pm_runtime_set_active(&pdev->dev);
  1360. pm_runtime_enable(&pdev->dev);
  1361. /* Register with the SPI framework */
  1362. platform_set_drvdata(pdev, drv_data);
  1363. status = devm_spi_register_master(&pdev->dev, master);
  1364. if (status != 0) {
  1365. dev_err(&pdev->dev, "problem registering spi master\n");
  1366. goto out_error_clock_enabled;
  1367. }
  1368. return status;
  1369. out_error_clock_enabled:
  1370. clk_disable_unprepare(ssp->clk);
  1371. pxa2xx_spi_dma_release(drv_data);
  1372. free_irq(ssp->irq, drv_data);
  1373. out_error_master_alloc:
  1374. spi_master_put(master);
  1375. pxa_ssp_free(ssp);
  1376. return status;
  1377. }
  1378. static int pxa2xx_spi_remove(struct platform_device *pdev)
  1379. {
  1380. struct driver_data *drv_data = platform_get_drvdata(pdev);
  1381. struct ssp_device *ssp;
  1382. if (!drv_data)
  1383. return 0;
  1384. ssp = drv_data->ssp;
  1385. pm_runtime_get_sync(&pdev->dev);
  1386. /* Disable the SSP at the peripheral and SOC level */
  1387. pxa2xx_spi_write(drv_data, SSCR0, 0);
  1388. clk_disable_unprepare(ssp->clk);
  1389. /* Release DMA */
  1390. if (drv_data->master_info->enable_dma)
  1391. pxa2xx_spi_dma_release(drv_data);
  1392. pm_runtime_put_noidle(&pdev->dev);
  1393. pm_runtime_disable(&pdev->dev);
  1394. /* Release IRQ */
  1395. free_irq(ssp->irq, drv_data);
  1396. /* Release SSP */
  1397. pxa_ssp_free(ssp);
  1398. return 0;
  1399. }
  1400. static void pxa2xx_spi_shutdown(struct platform_device *pdev)
  1401. {
  1402. int status = 0;
  1403. if ((status = pxa2xx_spi_remove(pdev)) != 0)
  1404. dev_err(&pdev->dev, "shutdown failed with %d\n", status);
  1405. }
  1406. #ifdef CONFIG_PM_SLEEP
  1407. static int pxa2xx_spi_suspend(struct device *dev)
  1408. {
  1409. struct driver_data *drv_data = dev_get_drvdata(dev);
  1410. struct ssp_device *ssp = drv_data->ssp;
  1411. int status = 0;
  1412. status = spi_master_suspend(drv_data->master);
  1413. if (status != 0)
  1414. return status;
  1415. pxa2xx_spi_write(drv_data, SSCR0, 0);
  1416. if (!pm_runtime_suspended(dev))
  1417. clk_disable_unprepare(ssp->clk);
  1418. return 0;
  1419. }
  1420. static int pxa2xx_spi_resume(struct device *dev)
  1421. {
  1422. struct driver_data *drv_data = dev_get_drvdata(dev);
  1423. struct ssp_device *ssp = drv_data->ssp;
  1424. int status = 0;
  1425. /* Enable the SSP clock */
  1426. if (!pm_runtime_suspended(dev))
  1427. clk_prepare_enable(ssp->clk);
  1428. /* Restore LPSS private register bits */
  1429. if (is_lpss_ssp(drv_data))
  1430. lpss_ssp_setup(drv_data);
  1431. /* Start the queue running */
  1432. status = spi_master_resume(drv_data->master);
  1433. if (status != 0) {
  1434. dev_err(dev, "problem starting queue (%d)\n", status);
  1435. return status;
  1436. }
  1437. return 0;
  1438. }
  1439. #endif
  1440. #ifdef CONFIG_PM
  1441. static int pxa2xx_spi_runtime_suspend(struct device *dev)
  1442. {
  1443. struct driver_data *drv_data = dev_get_drvdata(dev);
  1444. clk_disable_unprepare(drv_data->ssp->clk);
  1445. return 0;
  1446. }
  1447. static int pxa2xx_spi_runtime_resume(struct device *dev)
  1448. {
  1449. struct driver_data *drv_data = dev_get_drvdata(dev);
  1450. clk_prepare_enable(drv_data->ssp->clk);
  1451. return 0;
  1452. }
  1453. #endif
  1454. static const struct dev_pm_ops pxa2xx_spi_pm_ops = {
  1455. SET_SYSTEM_SLEEP_PM_OPS(pxa2xx_spi_suspend, pxa2xx_spi_resume)
  1456. SET_RUNTIME_PM_OPS(pxa2xx_spi_runtime_suspend,
  1457. pxa2xx_spi_runtime_resume, NULL)
  1458. };
  1459. static struct platform_driver driver = {
  1460. .driver = {
  1461. .name = "pxa2xx-spi",
  1462. .pm = &pxa2xx_spi_pm_ops,
  1463. .acpi_match_table = ACPI_PTR(pxa2xx_spi_acpi_match),
  1464. },
  1465. .probe = pxa2xx_spi_probe,
  1466. .remove = pxa2xx_spi_remove,
  1467. .shutdown = pxa2xx_spi_shutdown,
  1468. };
  1469. static int __init pxa2xx_spi_init(void)
  1470. {
  1471. return platform_driver_register(&driver);
  1472. }
  1473. subsys_initcall(pxa2xx_spi_init);
  1474. static void __exit pxa2xx_spi_exit(void)
  1475. {
  1476. platform_driver_unregister(&driver);
  1477. }
  1478. module_exit(pxa2xx_spi_exit);