i2c.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  1. /*
  2. * I2C Link Layer for PN544 HCI based Driver
  3. *
  4. * Copyright (C) 2012 Intel Corporation. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  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. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include <linux/crc-ccitt.h>
  20. #include <linux/module.h>
  21. #include <linux/i2c.h>
  22. #include <linux/gpio.h>
  23. #include <linux/of_gpio.h>
  24. #include <linux/of_irq.h>
  25. #include <linux/miscdevice.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/delay.h>
  28. #include <linux/nfc.h>
  29. #include <linux/firmware.h>
  30. #include <linux/unaligned/access_ok.h>
  31. #include <linux/platform_data/pn544.h>
  32. #include <net/nfc/hci.h>
  33. #include <net/nfc/llc.h>
  34. #include <net/nfc/nfc.h>
  35. #include "pn544.h"
  36. #define PN544_I2C_FRAME_HEADROOM 1
  37. #define PN544_I2C_FRAME_TAILROOM 2
  38. /* framing in HCI mode */
  39. #define PN544_HCI_I2C_LLC_LEN 1
  40. #define PN544_HCI_I2C_LLC_CRC 2
  41. #define PN544_HCI_I2C_LLC_LEN_CRC (PN544_HCI_I2C_LLC_LEN + \
  42. PN544_HCI_I2C_LLC_CRC)
  43. #define PN544_HCI_I2C_LLC_MIN_SIZE (1 + PN544_HCI_I2C_LLC_LEN_CRC)
  44. #define PN544_HCI_I2C_LLC_MAX_PAYLOAD 29
  45. #define PN544_HCI_I2C_LLC_MAX_SIZE (PN544_HCI_I2C_LLC_LEN_CRC + 1 + \
  46. PN544_HCI_I2C_LLC_MAX_PAYLOAD)
  47. static struct i2c_device_id pn544_hci_i2c_id_table[] = {
  48. {"pn544", 0},
  49. {}
  50. };
  51. MODULE_DEVICE_TABLE(i2c, pn544_hci_i2c_id_table);
  52. #define PN544_HCI_I2C_DRIVER_NAME "pn544_hci_i2c"
  53. /*
  54. * Exposed through the 4 most significant bytes
  55. * from the HCI SW_VERSION first byte, a.k.a.
  56. * SW RomLib.
  57. */
  58. #define PN544_HW_VARIANT_C2 0xa
  59. #define PN544_HW_VARIANT_C3 0xb
  60. #define PN544_FW_CMD_RESET 0x01
  61. #define PN544_FW_CMD_WRITE 0x08
  62. #define PN544_FW_CMD_CHECK 0x06
  63. #define PN544_FW_CMD_SECURE_WRITE 0x0C
  64. #define PN544_FW_CMD_SECURE_CHUNK_WRITE 0x0D
  65. struct pn544_i2c_fw_frame_write {
  66. u8 cmd;
  67. u16 be_length;
  68. u8 be_dest_addr[3];
  69. u16 be_datalen;
  70. u8 data[];
  71. } __packed;
  72. struct pn544_i2c_fw_frame_check {
  73. u8 cmd;
  74. u16 be_length;
  75. u8 be_start_addr[3];
  76. u16 be_datalen;
  77. u16 be_crc;
  78. } __packed;
  79. struct pn544_i2c_fw_frame_response {
  80. u8 status;
  81. u16 be_length;
  82. } __packed;
  83. struct pn544_i2c_fw_blob {
  84. u32 be_size;
  85. u32 be_destaddr;
  86. u8 data[];
  87. };
  88. struct pn544_i2c_fw_secure_frame {
  89. u8 cmd;
  90. u16 be_datalen;
  91. u8 data[];
  92. } __packed;
  93. struct pn544_i2c_fw_secure_blob {
  94. u64 header;
  95. u8 data[];
  96. };
  97. #define PN544_FW_CMD_RESULT_TIMEOUT 0x01
  98. #define PN544_FW_CMD_RESULT_BAD_CRC 0x02
  99. #define PN544_FW_CMD_RESULT_ACCESS_DENIED 0x08
  100. #define PN544_FW_CMD_RESULT_PROTOCOL_ERROR 0x0B
  101. #define PN544_FW_CMD_RESULT_INVALID_PARAMETER 0x11
  102. #define PN544_FW_CMD_RESULT_UNSUPPORTED_COMMAND 0x13
  103. #define PN544_FW_CMD_RESULT_INVALID_LENGTH 0x18
  104. #define PN544_FW_CMD_RESULT_CRYPTOGRAPHIC_ERROR 0x19
  105. #define PN544_FW_CMD_RESULT_VERSION_CONDITIONS_ERROR 0x1D
  106. #define PN544_FW_CMD_RESULT_MEMORY_ERROR 0x20
  107. #define PN544_FW_CMD_RESULT_CHUNK_OK 0x21
  108. #define PN544_FW_CMD_RESULT_WRITE_FAILED 0x74
  109. #define PN544_FW_CMD_RESULT_COMMAND_REJECTED 0xE0
  110. #define PN544_FW_CMD_RESULT_CHUNK_ERROR 0xE6
  111. #define MIN(X, Y) ((X) < (Y) ? (X) : (Y))
  112. #define PN544_FW_WRITE_BUFFER_MAX_LEN 0x9f7
  113. #define PN544_FW_I2C_MAX_PAYLOAD PN544_HCI_I2C_LLC_MAX_SIZE
  114. #define PN544_FW_I2C_WRITE_FRAME_HEADER_LEN 8
  115. #define PN544_FW_I2C_WRITE_DATA_MAX_LEN MIN((PN544_FW_I2C_MAX_PAYLOAD -\
  116. PN544_FW_I2C_WRITE_FRAME_HEADER_LEN),\
  117. PN544_FW_WRITE_BUFFER_MAX_LEN)
  118. #define PN544_FW_SECURE_CHUNK_WRITE_HEADER_LEN 3
  119. #define PN544_FW_SECURE_CHUNK_WRITE_DATA_MAX_LEN (PN544_FW_I2C_MAX_PAYLOAD -\
  120. PN544_FW_SECURE_CHUNK_WRITE_HEADER_LEN)
  121. #define PN544_FW_SECURE_FRAME_HEADER_LEN 3
  122. #define PN544_FW_SECURE_BLOB_HEADER_LEN 8
  123. #define FW_WORK_STATE_IDLE 1
  124. #define FW_WORK_STATE_START 2
  125. #define FW_WORK_STATE_WAIT_WRITE_ANSWER 3
  126. #define FW_WORK_STATE_WAIT_CHECK_ANSWER 4
  127. #define FW_WORK_STATE_WAIT_SECURE_WRITE_ANSWER 5
  128. struct pn544_i2c_phy {
  129. struct i2c_client *i2c_dev;
  130. struct nfc_hci_dev *hdev;
  131. unsigned int gpio_en;
  132. unsigned int gpio_irq;
  133. unsigned int gpio_fw;
  134. unsigned int en_polarity;
  135. u8 hw_variant;
  136. struct work_struct fw_work;
  137. int fw_work_state;
  138. char firmware_name[NFC_FIRMWARE_NAME_MAXSIZE + 1];
  139. const struct firmware *fw;
  140. u32 fw_blob_dest_addr;
  141. size_t fw_blob_size;
  142. const u8 *fw_blob_data;
  143. size_t fw_written;
  144. size_t fw_size;
  145. int fw_cmd_result;
  146. int powered;
  147. int run_mode;
  148. int hard_fault; /*
  149. * < 0 if hardware error occured (e.g. i2c err)
  150. * and prevents normal operation.
  151. */
  152. };
  153. #define I2C_DUMP_SKB(info, skb) \
  154. do { \
  155. pr_debug("%s:\n", info); \
  156. print_hex_dump(KERN_DEBUG, "i2c: ", DUMP_PREFIX_OFFSET, \
  157. 16, 1, (skb)->data, (skb)->len, 0); \
  158. } while (0)
  159. static void pn544_hci_i2c_platform_init(struct pn544_i2c_phy *phy)
  160. {
  161. int polarity, retry, ret;
  162. char rset_cmd[] = { 0x05, 0xF9, 0x04, 0x00, 0xC3, 0xE5 };
  163. int count = sizeof(rset_cmd);
  164. nfc_info(&phy->i2c_dev->dev, "Detecting nfc_en polarity\n");
  165. /* Disable fw download */
  166. gpio_set_value(phy->gpio_fw, 0);
  167. for (polarity = 0; polarity < 2; polarity++) {
  168. phy->en_polarity = polarity;
  169. retry = 3;
  170. while (retry--) {
  171. /* power off */
  172. gpio_set_value(phy->gpio_en, !phy->en_polarity);
  173. usleep_range(10000, 15000);
  174. /* power on */
  175. gpio_set_value(phy->gpio_en, phy->en_polarity);
  176. usleep_range(10000, 15000);
  177. /* send reset */
  178. dev_dbg(&phy->i2c_dev->dev, "Sending reset cmd\n");
  179. ret = i2c_master_send(phy->i2c_dev, rset_cmd, count);
  180. if (ret == count) {
  181. nfc_info(&phy->i2c_dev->dev,
  182. "nfc_en polarity : active %s\n",
  183. (polarity == 0 ? "low" : "high"));
  184. goto out;
  185. }
  186. }
  187. }
  188. nfc_err(&phy->i2c_dev->dev,
  189. "Could not detect nfc_en polarity, fallback to active high\n");
  190. out:
  191. gpio_set_value(phy->gpio_en, !phy->en_polarity);
  192. }
  193. static void pn544_hci_i2c_enable_mode(struct pn544_i2c_phy *phy, int run_mode)
  194. {
  195. gpio_set_value(phy->gpio_fw, run_mode == PN544_FW_MODE ? 1 : 0);
  196. gpio_set_value(phy->gpio_en, phy->en_polarity);
  197. usleep_range(10000, 15000);
  198. phy->run_mode = run_mode;
  199. }
  200. static int pn544_hci_i2c_enable(void *phy_id)
  201. {
  202. struct pn544_i2c_phy *phy = phy_id;
  203. pr_info("%s\n", __func__);
  204. pn544_hci_i2c_enable_mode(phy, PN544_HCI_MODE);
  205. phy->powered = 1;
  206. return 0;
  207. }
  208. static void pn544_hci_i2c_disable(void *phy_id)
  209. {
  210. struct pn544_i2c_phy *phy = phy_id;
  211. gpio_set_value(phy->gpio_fw, 0);
  212. gpio_set_value(phy->gpio_en, !phy->en_polarity);
  213. usleep_range(10000, 15000);
  214. gpio_set_value(phy->gpio_en, phy->en_polarity);
  215. usleep_range(10000, 15000);
  216. gpio_set_value(phy->gpio_en, !phy->en_polarity);
  217. usleep_range(10000, 15000);
  218. phy->powered = 0;
  219. }
  220. static void pn544_hci_i2c_add_len_crc(struct sk_buff *skb)
  221. {
  222. u16 crc;
  223. int len;
  224. len = skb->len + 2;
  225. *skb_push(skb, 1) = len;
  226. crc = crc_ccitt(0xffff, skb->data, skb->len);
  227. crc = ~crc;
  228. *skb_put(skb, 1) = crc & 0xff;
  229. *skb_put(skb, 1) = crc >> 8;
  230. }
  231. static void pn544_hci_i2c_remove_len_crc(struct sk_buff *skb)
  232. {
  233. skb_pull(skb, PN544_I2C_FRAME_HEADROOM);
  234. skb_trim(skb, PN544_I2C_FRAME_TAILROOM);
  235. }
  236. /*
  237. * Writing a frame must not return the number of written bytes.
  238. * It must return either zero for success, or <0 for error.
  239. * In addition, it must not alter the skb
  240. */
  241. static int pn544_hci_i2c_write(void *phy_id, struct sk_buff *skb)
  242. {
  243. int r;
  244. struct pn544_i2c_phy *phy = phy_id;
  245. struct i2c_client *client = phy->i2c_dev;
  246. if (phy->hard_fault != 0)
  247. return phy->hard_fault;
  248. usleep_range(3000, 6000);
  249. pn544_hci_i2c_add_len_crc(skb);
  250. I2C_DUMP_SKB("i2c frame written", skb);
  251. r = i2c_master_send(client, skb->data, skb->len);
  252. if (r == -EREMOTEIO) { /* Retry, chip was in standby */
  253. usleep_range(6000, 10000);
  254. r = i2c_master_send(client, skb->data, skb->len);
  255. }
  256. if (r >= 0) {
  257. if (r != skb->len)
  258. r = -EREMOTEIO;
  259. else
  260. r = 0;
  261. }
  262. pn544_hci_i2c_remove_len_crc(skb);
  263. return r;
  264. }
  265. static int check_crc(u8 *buf, int buflen)
  266. {
  267. int len;
  268. u16 crc;
  269. len = buf[0] + 1;
  270. crc = crc_ccitt(0xffff, buf, len - 2);
  271. crc = ~crc;
  272. if (buf[len - 2] != (crc & 0xff) || buf[len - 1] != (crc >> 8)) {
  273. pr_err("CRC error 0x%x != 0x%x 0x%x\n",
  274. crc, buf[len - 1], buf[len - 2]);
  275. pr_info("%s: BAD CRC\n", __func__);
  276. print_hex_dump(KERN_DEBUG, "crc: ", DUMP_PREFIX_NONE,
  277. 16, 2, buf, buflen, false);
  278. return -EPERM;
  279. }
  280. return 0;
  281. }
  282. /*
  283. * Reads an shdlc frame and returns it in a newly allocated sk_buff. Guarantees
  284. * that i2c bus will be flushed and that next read will start on a new frame.
  285. * returned skb contains only LLC header and payload.
  286. * returns:
  287. * -EREMOTEIO : i2c read error (fatal)
  288. * -EBADMSG : frame was incorrect and discarded
  289. * -ENOMEM : cannot allocate skb, frame dropped
  290. */
  291. static int pn544_hci_i2c_read(struct pn544_i2c_phy *phy, struct sk_buff **skb)
  292. {
  293. int r;
  294. u8 len;
  295. u8 tmp[PN544_HCI_I2C_LLC_MAX_SIZE - 1];
  296. struct i2c_client *client = phy->i2c_dev;
  297. r = i2c_master_recv(client, &len, 1);
  298. if (r != 1) {
  299. nfc_err(&client->dev, "cannot read len byte\n");
  300. return -EREMOTEIO;
  301. }
  302. if ((len < (PN544_HCI_I2C_LLC_MIN_SIZE - 1)) ||
  303. (len > (PN544_HCI_I2C_LLC_MAX_SIZE - 1))) {
  304. nfc_err(&client->dev, "invalid len byte\n");
  305. r = -EBADMSG;
  306. goto flush;
  307. }
  308. *skb = alloc_skb(1 + len, GFP_KERNEL);
  309. if (*skb == NULL) {
  310. r = -ENOMEM;
  311. goto flush;
  312. }
  313. *skb_put(*skb, 1) = len;
  314. r = i2c_master_recv(client, skb_put(*skb, len), len);
  315. if (r != len) {
  316. kfree_skb(*skb);
  317. return -EREMOTEIO;
  318. }
  319. I2C_DUMP_SKB("i2c frame read", *skb);
  320. r = check_crc((*skb)->data, (*skb)->len);
  321. if (r != 0) {
  322. kfree_skb(*skb);
  323. r = -EBADMSG;
  324. goto flush;
  325. }
  326. skb_pull(*skb, 1);
  327. skb_trim(*skb, (*skb)->len - 2);
  328. usleep_range(3000, 6000);
  329. return 0;
  330. flush:
  331. if (i2c_master_recv(client, tmp, sizeof(tmp)) < 0)
  332. r = -EREMOTEIO;
  333. usleep_range(3000, 6000);
  334. return r;
  335. }
  336. static int pn544_hci_i2c_fw_read_status(struct pn544_i2c_phy *phy)
  337. {
  338. int r;
  339. struct pn544_i2c_fw_frame_response response;
  340. struct i2c_client *client = phy->i2c_dev;
  341. r = i2c_master_recv(client, (char *) &response, sizeof(response));
  342. if (r != sizeof(response)) {
  343. nfc_err(&client->dev, "cannot read fw status\n");
  344. return -EIO;
  345. }
  346. usleep_range(3000, 6000);
  347. switch (response.status) {
  348. case 0:
  349. return 0;
  350. case PN544_FW_CMD_RESULT_CHUNK_OK:
  351. return response.status;
  352. case PN544_FW_CMD_RESULT_TIMEOUT:
  353. return -ETIMEDOUT;
  354. case PN544_FW_CMD_RESULT_BAD_CRC:
  355. return -ENODATA;
  356. case PN544_FW_CMD_RESULT_ACCESS_DENIED:
  357. return -EACCES;
  358. case PN544_FW_CMD_RESULT_PROTOCOL_ERROR:
  359. return -EPROTO;
  360. case PN544_FW_CMD_RESULT_INVALID_PARAMETER:
  361. return -EINVAL;
  362. case PN544_FW_CMD_RESULT_UNSUPPORTED_COMMAND:
  363. return -ENOTSUPP;
  364. case PN544_FW_CMD_RESULT_INVALID_LENGTH:
  365. return -EBADMSG;
  366. case PN544_FW_CMD_RESULT_CRYPTOGRAPHIC_ERROR:
  367. return -ENOKEY;
  368. case PN544_FW_CMD_RESULT_VERSION_CONDITIONS_ERROR:
  369. return -EINVAL;
  370. case PN544_FW_CMD_RESULT_MEMORY_ERROR:
  371. return -ENOMEM;
  372. case PN544_FW_CMD_RESULT_COMMAND_REJECTED:
  373. return -EACCES;
  374. case PN544_FW_CMD_RESULT_WRITE_FAILED:
  375. case PN544_FW_CMD_RESULT_CHUNK_ERROR:
  376. return -EIO;
  377. default:
  378. return -EIO;
  379. }
  380. }
  381. /*
  382. * Reads an shdlc frame from the chip. This is not as straightforward as it
  383. * seems. There are cases where we could loose the frame start synchronization.
  384. * The frame format is len-data-crc, and corruption can occur anywhere while
  385. * transiting on i2c bus, such that we could read an invalid len.
  386. * In order to recover synchronization with the next frame, we must be sure
  387. * to read the real amount of data without using the len byte. We do this by
  388. * assuming the following:
  389. * - the chip will always present only one single complete frame on the bus
  390. * before triggering the interrupt
  391. * - the chip will not present a new frame until we have completely read
  392. * the previous one (or until we have handled the interrupt).
  393. * The tricky case is when we read a corrupted len that is less than the real
  394. * len. We must detect this here in order to determine that we need to flush
  395. * the bus. This is the reason why we check the crc here.
  396. */
  397. static irqreturn_t pn544_hci_i2c_irq_thread_fn(int irq, void *phy_id)
  398. {
  399. struct pn544_i2c_phy *phy = phy_id;
  400. struct i2c_client *client;
  401. struct sk_buff *skb = NULL;
  402. int r;
  403. if (!phy || irq != phy->i2c_dev->irq) {
  404. WARN_ON_ONCE(1);
  405. return IRQ_NONE;
  406. }
  407. client = phy->i2c_dev;
  408. dev_dbg(&client->dev, "IRQ\n");
  409. if (phy->hard_fault != 0)
  410. return IRQ_HANDLED;
  411. if (phy->run_mode == PN544_FW_MODE) {
  412. phy->fw_cmd_result = pn544_hci_i2c_fw_read_status(phy);
  413. schedule_work(&phy->fw_work);
  414. } else {
  415. r = pn544_hci_i2c_read(phy, &skb);
  416. if (r == -EREMOTEIO) {
  417. phy->hard_fault = r;
  418. nfc_hci_recv_frame(phy->hdev, NULL);
  419. return IRQ_HANDLED;
  420. } else if ((r == -ENOMEM) || (r == -EBADMSG)) {
  421. return IRQ_HANDLED;
  422. }
  423. nfc_hci_recv_frame(phy->hdev, skb);
  424. }
  425. return IRQ_HANDLED;
  426. }
  427. static struct nfc_phy_ops i2c_phy_ops = {
  428. .write = pn544_hci_i2c_write,
  429. .enable = pn544_hci_i2c_enable,
  430. .disable = pn544_hci_i2c_disable,
  431. };
  432. static int pn544_hci_i2c_fw_download(void *phy_id, const char *firmware_name,
  433. u8 hw_variant)
  434. {
  435. struct pn544_i2c_phy *phy = phy_id;
  436. pr_info("Starting Firmware Download (%s)\n", firmware_name);
  437. strcpy(phy->firmware_name, firmware_name);
  438. phy->hw_variant = hw_variant;
  439. phy->fw_work_state = FW_WORK_STATE_START;
  440. schedule_work(&phy->fw_work);
  441. return 0;
  442. }
  443. static void pn544_hci_i2c_fw_work_complete(struct pn544_i2c_phy *phy,
  444. int result)
  445. {
  446. pr_info("Firmware Download Complete, result=%d\n", result);
  447. pn544_hci_i2c_disable(phy);
  448. phy->fw_work_state = FW_WORK_STATE_IDLE;
  449. if (phy->fw) {
  450. release_firmware(phy->fw);
  451. phy->fw = NULL;
  452. }
  453. nfc_fw_download_done(phy->hdev->ndev, phy->firmware_name, (u32) -result);
  454. }
  455. static int pn544_hci_i2c_fw_write_cmd(struct i2c_client *client, u32 dest_addr,
  456. const u8 *data, u16 datalen)
  457. {
  458. u8 frame[PN544_FW_I2C_MAX_PAYLOAD];
  459. struct pn544_i2c_fw_frame_write *framep;
  460. u16 params_len;
  461. int framelen;
  462. int r;
  463. if (datalen > PN544_FW_I2C_WRITE_DATA_MAX_LEN)
  464. datalen = PN544_FW_I2C_WRITE_DATA_MAX_LEN;
  465. framep = (struct pn544_i2c_fw_frame_write *) frame;
  466. params_len = sizeof(framep->be_dest_addr) +
  467. sizeof(framep->be_datalen) + datalen;
  468. framelen = params_len + sizeof(framep->cmd) +
  469. sizeof(framep->be_length);
  470. framep->cmd = PN544_FW_CMD_WRITE;
  471. put_unaligned_be16(params_len, &framep->be_length);
  472. framep->be_dest_addr[0] = (dest_addr & 0xff0000) >> 16;
  473. framep->be_dest_addr[1] = (dest_addr & 0xff00) >> 8;
  474. framep->be_dest_addr[2] = dest_addr & 0xff;
  475. put_unaligned_be16(datalen, &framep->be_datalen);
  476. memcpy(framep->data, data, datalen);
  477. r = i2c_master_send(client, frame, framelen);
  478. if (r == framelen)
  479. return datalen;
  480. else if (r < 0)
  481. return r;
  482. else
  483. return -EIO;
  484. }
  485. static int pn544_hci_i2c_fw_check_cmd(struct i2c_client *client, u32 start_addr,
  486. const u8 *data, u16 datalen)
  487. {
  488. struct pn544_i2c_fw_frame_check frame;
  489. int r;
  490. u16 crc;
  491. /* calculate local crc for the data we want to check */
  492. crc = crc_ccitt(0xffff, data, datalen);
  493. frame.cmd = PN544_FW_CMD_CHECK;
  494. put_unaligned_be16(sizeof(frame.be_start_addr) +
  495. sizeof(frame.be_datalen) + sizeof(frame.be_crc),
  496. &frame.be_length);
  497. /* tell the chip the memory region to which our crc applies */
  498. frame.be_start_addr[0] = (start_addr & 0xff0000) >> 16;
  499. frame.be_start_addr[1] = (start_addr & 0xff00) >> 8;
  500. frame.be_start_addr[2] = start_addr & 0xff;
  501. put_unaligned_be16(datalen, &frame.be_datalen);
  502. /*
  503. * and give our local crc. Chip will calculate its own crc for the
  504. * region and compare with ours.
  505. */
  506. put_unaligned_be16(crc, &frame.be_crc);
  507. r = i2c_master_send(client, (const char *) &frame, sizeof(frame));
  508. if (r == sizeof(frame))
  509. return 0;
  510. else if (r < 0)
  511. return r;
  512. else
  513. return -EIO;
  514. }
  515. static int pn544_hci_i2c_fw_write_chunk(struct pn544_i2c_phy *phy)
  516. {
  517. int r;
  518. r = pn544_hci_i2c_fw_write_cmd(phy->i2c_dev,
  519. phy->fw_blob_dest_addr + phy->fw_written,
  520. phy->fw_blob_data + phy->fw_written,
  521. phy->fw_blob_size - phy->fw_written);
  522. if (r < 0)
  523. return r;
  524. phy->fw_written += r;
  525. phy->fw_work_state = FW_WORK_STATE_WAIT_WRITE_ANSWER;
  526. return 0;
  527. }
  528. static int pn544_hci_i2c_fw_secure_write_frame_cmd(struct pn544_i2c_phy *phy,
  529. const u8 *data, u16 datalen)
  530. {
  531. u8 buf[PN544_FW_I2C_MAX_PAYLOAD];
  532. struct pn544_i2c_fw_secure_frame *chunk;
  533. int chunklen;
  534. int r;
  535. if (datalen > PN544_FW_SECURE_CHUNK_WRITE_DATA_MAX_LEN)
  536. datalen = PN544_FW_SECURE_CHUNK_WRITE_DATA_MAX_LEN;
  537. chunk = (struct pn544_i2c_fw_secure_frame *) buf;
  538. chunk->cmd = PN544_FW_CMD_SECURE_CHUNK_WRITE;
  539. put_unaligned_be16(datalen, &chunk->be_datalen);
  540. memcpy(chunk->data, data, datalen);
  541. chunklen = sizeof(chunk->cmd) + sizeof(chunk->be_datalen) + datalen;
  542. r = i2c_master_send(phy->i2c_dev, buf, chunklen);
  543. if (r == chunklen)
  544. return datalen;
  545. else if (r < 0)
  546. return r;
  547. else
  548. return -EIO;
  549. }
  550. static int pn544_hci_i2c_fw_secure_write_frame(struct pn544_i2c_phy *phy)
  551. {
  552. struct pn544_i2c_fw_secure_frame *framep;
  553. int r;
  554. framep = (struct pn544_i2c_fw_secure_frame *) phy->fw_blob_data;
  555. if (phy->fw_written == 0)
  556. phy->fw_blob_size = get_unaligned_be16(&framep->be_datalen)
  557. + PN544_FW_SECURE_FRAME_HEADER_LEN;
  558. /* Only secure write command can be chunked*/
  559. if (phy->fw_blob_size > PN544_FW_I2C_MAX_PAYLOAD &&
  560. framep->cmd != PN544_FW_CMD_SECURE_WRITE)
  561. return -EINVAL;
  562. /* The firmware also have other commands, we just send them directly */
  563. if (phy->fw_blob_size < PN544_FW_I2C_MAX_PAYLOAD) {
  564. r = i2c_master_send(phy->i2c_dev,
  565. (const char *) phy->fw_blob_data, phy->fw_blob_size);
  566. if (r == phy->fw_blob_size)
  567. goto exit;
  568. else if (r < 0)
  569. return r;
  570. else
  571. return -EIO;
  572. }
  573. r = pn544_hci_i2c_fw_secure_write_frame_cmd(phy,
  574. phy->fw_blob_data + phy->fw_written,
  575. phy->fw_blob_size - phy->fw_written);
  576. if (r < 0)
  577. return r;
  578. exit:
  579. phy->fw_written += r;
  580. phy->fw_work_state = FW_WORK_STATE_WAIT_SECURE_WRITE_ANSWER;
  581. /* SW reset command will not trig any response from PN544 */
  582. if (framep->cmd == PN544_FW_CMD_RESET) {
  583. pn544_hci_i2c_enable_mode(phy, PN544_FW_MODE);
  584. phy->fw_cmd_result = 0;
  585. schedule_work(&phy->fw_work);
  586. }
  587. return 0;
  588. }
  589. static void pn544_hci_i2c_fw_work(struct work_struct *work)
  590. {
  591. struct pn544_i2c_phy *phy = container_of(work, struct pn544_i2c_phy,
  592. fw_work);
  593. int r;
  594. struct pn544_i2c_fw_blob *blob;
  595. struct pn544_i2c_fw_secure_blob *secure_blob;
  596. switch (phy->fw_work_state) {
  597. case FW_WORK_STATE_START:
  598. pn544_hci_i2c_enable_mode(phy, PN544_FW_MODE);
  599. r = request_firmware(&phy->fw, phy->firmware_name,
  600. &phy->i2c_dev->dev);
  601. if (r < 0)
  602. goto exit_state_start;
  603. phy->fw_written = 0;
  604. switch (phy->hw_variant) {
  605. case PN544_HW_VARIANT_C2:
  606. blob = (struct pn544_i2c_fw_blob *) phy->fw->data;
  607. phy->fw_blob_size = get_unaligned_be32(&blob->be_size);
  608. phy->fw_blob_dest_addr = get_unaligned_be32(
  609. &blob->be_destaddr);
  610. phy->fw_blob_data = blob->data;
  611. r = pn544_hci_i2c_fw_write_chunk(phy);
  612. break;
  613. case PN544_HW_VARIANT_C3:
  614. secure_blob = (struct pn544_i2c_fw_secure_blob *)
  615. phy->fw->data;
  616. phy->fw_blob_data = secure_blob->data;
  617. phy->fw_size = phy->fw->size;
  618. r = pn544_hci_i2c_fw_secure_write_frame(phy);
  619. break;
  620. default:
  621. r = -ENOTSUPP;
  622. break;
  623. }
  624. exit_state_start:
  625. if (r < 0)
  626. pn544_hci_i2c_fw_work_complete(phy, r);
  627. break;
  628. case FW_WORK_STATE_WAIT_WRITE_ANSWER:
  629. r = phy->fw_cmd_result;
  630. if (r < 0)
  631. goto exit_state_wait_write_answer;
  632. if (phy->fw_written == phy->fw_blob_size) {
  633. r = pn544_hci_i2c_fw_check_cmd(phy->i2c_dev,
  634. phy->fw_blob_dest_addr,
  635. phy->fw_blob_data,
  636. phy->fw_blob_size);
  637. if (r < 0)
  638. goto exit_state_wait_write_answer;
  639. phy->fw_work_state = FW_WORK_STATE_WAIT_CHECK_ANSWER;
  640. break;
  641. }
  642. r = pn544_hci_i2c_fw_write_chunk(phy);
  643. exit_state_wait_write_answer:
  644. if (r < 0)
  645. pn544_hci_i2c_fw_work_complete(phy, r);
  646. break;
  647. case FW_WORK_STATE_WAIT_CHECK_ANSWER:
  648. r = phy->fw_cmd_result;
  649. if (r < 0)
  650. goto exit_state_wait_check_answer;
  651. blob = (struct pn544_i2c_fw_blob *) (phy->fw_blob_data +
  652. phy->fw_blob_size);
  653. phy->fw_blob_size = get_unaligned_be32(&blob->be_size);
  654. if (phy->fw_blob_size != 0) {
  655. phy->fw_blob_dest_addr =
  656. get_unaligned_be32(&blob->be_destaddr);
  657. phy->fw_blob_data = blob->data;
  658. phy->fw_written = 0;
  659. r = pn544_hci_i2c_fw_write_chunk(phy);
  660. }
  661. exit_state_wait_check_answer:
  662. if (r < 0 || phy->fw_blob_size == 0)
  663. pn544_hci_i2c_fw_work_complete(phy, r);
  664. break;
  665. case FW_WORK_STATE_WAIT_SECURE_WRITE_ANSWER:
  666. r = phy->fw_cmd_result;
  667. if (r < 0)
  668. goto exit_state_wait_secure_write_answer;
  669. if (r == PN544_FW_CMD_RESULT_CHUNK_OK) {
  670. r = pn544_hci_i2c_fw_secure_write_frame(phy);
  671. goto exit_state_wait_secure_write_answer;
  672. }
  673. if (phy->fw_written == phy->fw_blob_size) {
  674. secure_blob = (struct pn544_i2c_fw_secure_blob *)
  675. (phy->fw_blob_data + phy->fw_blob_size);
  676. phy->fw_size -= phy->fw_blob_size +
  677. PN544_FW_SECURE_BLOB_HEADER_LEN;
  678. if (phy->fw_size >= PN544_FW_SECURE_BLOB_HEADER_LEN
  679. + PN544_FW_SECURE_FRAME_HEADER_LEN) {
  680. phy->fw_blob_data = secure_blob->data;
  681. phy->fw_written = 0;
  682. r = pn544_hci_i2c_fw_secure_write_frame(phy);
  683. }
  684. }
  685. exit_state_wait_secure_write_answer:
  686. if (r < 0 || phy->fw_size == 0)
  687. pn544_hci_i2c_fw_work_complete(phy, r);
  688. break;
  689. default:
  690. break;
  691. }
  692. }
  693. #ifdef CONFIG_OF
  694. static int pn544_hci_i2c_of_request_resources(struct i2c_client *client)
  695. {
  696. struct pn544_i2c_phy *phy = i2c_get_clientdata(client);
  697. struct device_node *pp;
  698. int ret;
  699. pp = client->dev.of_node;
  700. if (!pp) {
  701. ret = -ENODEV;
  702. goto err_dt;
  703. }
  704. /* Obtention of EN GPIO from device tree */
  705. ret = of_get_named_gpio(pp, "enable-gpios", 0);
  706. if (ret < 0) {
  707. if (ret != -EPROBE_DEFER)
  708. nfc_err(&client->dev,
  709. "Failed to get EN gpio, error: %d\n", ret);
  710. goto err_dt;
  711. }
  712. phy->gpio_en = ret;
  713. /* Configuration of EN GPIO */
  714. ret = gpio_request(phy->gpio_en, "pn544_en");
  715. if (ret) {
  716. nfc_err(&client->dev, "Fail EN pin\n");
  717. goto err_dt;
  718. }
  719. ret = gpio_direction_output(phy->gpio_en, 0);
  720. if (ret) {
  721. nfc_err(&client->dev, "Fail EN pin direction\n");
  722. goto err_gpio_en;
  723. }
  724. /* Obtention of FW GPIO from device tree */
  725. ret = of_get_named_gpio(pp, "firmware-gpios", 0);
  726. if (ret < 0) {
  727. if (ret != -EPROBE_DEFER)
  728. nfc_err(&client->dev,
  729. "Failed to get FW gpio, error: %d\n", ret);
  730. goto err_gpio_en;
  731. }
  732. phy->gpio_fw = ret;
  733. /* Configuration of FW GPIO */
  734. ret = gpio_request(phy->gpio_fw, "pn544_fw");
  735. if (ret) {
  736. nfc_err(&client->dev, "Fail FW pin\n");
  737. goto err_gpio_en;
  738. }
  739. ret = gpio_direction_output(phy->gpio_fw, 0);
  740. if (ret) {
  741. nfc_err(&client->dev, "Fail FW pin direction\n");
  742. goto err_gpio_fw;
  743. }
  744. /* IRQ */
  745. ret = irq_of_parse_and_map(pp, 0);
  746. if (ret < 0) {
  747. nfc_err(&client->dev,
  748. "Unable to get irq, error: %d\n", ret);
  749. goto err_gpio_fw;
  750. }
  751. client->irq = ret;
  752. return 0;
  753. err_gpio_fw:
  754. gpio_free(phy->gpio_fw);
  755. err_gpio_en:
  756. gpio_free(phy->gpio_en);
  757. err_dt:
  758. return ret;
  759. }
  760. #else
  761. static int pn544_hci_i2c_of_request_resources(struct i2c_client *client)
  762. {
  763. return -ENODEV;
  764. }
  765. #endif
  766. static int pn544_hci_i2c_probe(struct i2c_client *client,
  767. const struct i2c_device_id *id)
  768. {
  769. struct pn544_i2c_phy *phy;
  770. struct pn544_nfc_platform_data *pdata;
  771. int r = 0;
  772. dev_dbg(&client->dev, "%s\n", __func__);
  773. dev_dbg(&client->dev, "IRQ: %d\n", client->irq);
  774. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  775. nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
  776. return -ENODEV;
  777. }
  778. phy = devm_kzalloc(&client->dev, sizeof(struct pn544_i2c_phy),
  779. GFP_KERNEL);
  780. if (!phy) {
  781. nfc_err(&client->dev,
  782. "Cannot allocate memory for pn544 i2c phy.\n");
  783. return -ENOMEM;
  784. }
  785. INIT_WORK(&phy->fw_work, pn544_hci_i2c_fw_work);
  786. phy->fw_work_state = FW_WORK_STATE_IDLE;
  787. phy->i2c_dev = client;
  788. i2c_set_clientdata(client, phy);
  789. pdata = client->dev.platform_data;
  790. /* No platform data, using device tree. */
  791. if (!pdata && client->dev.of_node) {
  792. r = pn544_hci_i2c_of_request_resources(client);
  793. if (r) {
  794. nfc_err(&client->dev, "No DT data\n");
  795. return r;
  796. }
  797. /* Using platform data. */
  798. } else if (pdata) {
  799. if (pdata->request_resources == NULL) {
  800. nfc_err(&client->dev, "request_resources() missing\n");
  801. return -EINVAL;
  802. }
  803. r = pdata->request_resources(client);
  804. if (r) {
  805. nfc_err(&client->dev,
  806. "Cannot get platform resources\n");
  807. return r;
  808. }
  809. phy->gpio_en = pdata->get_gpio(NFC_GPIO_ENABLE);
  810. phy->gpio_fw = pdata->get_gpio(NFC_GPIO_FW_RESET);
  811. phy->gpio_irq = pdata->get_gpio(NFC_GPIO_IRQ);
  812. } else {
  813. nfc_err(&client->dev, "No platform data\n");
  814. return -EINVAL;
  815. }
  816. pn544_hci_i2c_platform_init(phy);
  817. r = request_threaded_irq(client->irq, NULL, pn544_hci_i2c_irq_thread_fn,
  818. IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  819. PN544_HCI_I2C_DRIVER_NAME, phy);
  820. if (r < 0) {
  821. nfc_err(&client->dev, "Unable to register IRQ handler\n");
  822. goto err_rti;
  823. }
  824. r = pn544_hci_probe(phy, &i2c_phy_ops, LLC_SHDLC_NAME,
  825. PN544_I2C_FRAME_HEADROOM, PN544_I2C_FRAME_TAILROOM,
  826. PN544_HCI_I2C_LLC_MAX_PAYLOAD,
  827. pn544_hci_i2c_fw_download, &phy->hdev);
  828. if (r < 0)
  829. goto err_hci;
  830. return 0;
  831. err_hci:
  832. free_irq(client->irq, phy);
  833. err_rti:
  834. if (!pdata) {
  835. gpio_free(phy->gpio_en);
  836. gpio_free(phy->gpio_fw);
  837. } else if (pdata->free_resources) {
  838. pdata->free_resources();
  839. }
  840. return r;
  841. }
  842. static int pn544_hci_i2c_remove(struct i2c_client *client)
  843. {
  844. struct pn544_i2c_phy *phy = i2c_get_clientdata(client);
  845. struct pn544_nfc_platform_data *pdata = client->dev.platform_data;
  846. dev_dbg(&client->dev, "%s\n", __func__);
  847. cancel_work_sync(&phy->fw_work);
  848. if (phy->fw_work_state != FW_WORK_STATE_IDLE)
  849. pn544_hci_i2c_fw_work_complete(phy, -ENODEV);
  850. pn544_hci_remove(phy->hdev);
  851. if (phy->powered)
  852. pn544_hci_i2c_disable(phy);
  853. free_irq(client->irq, phy);
  854. /* No platform data, GPIOs have been requested by this driver */
  855. if (!pdata) {
  856. gpio_free(phy->gpio_en);
  857. gpio_free(phy->gpio_fw);
  858. /* Using platform data */
  859. } else if (pdata->free_resources) {
  860. pdata->free_resources();
  861. }
  862. return 0;
  863. }
  864. static const struct of_device_id of_pn544_i2c_match[] = {
  865. { .compatible = "nxp,pn544-i2c", },
  866. {},
  867. };
  868. MODULE_DEVICE_TABLE(of, of_pn544_i2c_match);
  869. static struct i2c_driver pn544_hci_i2c_driver = {
  870. .driver = {
  871. .name = PN544_HCI_I2C_DRIVER_NAME,
  872. .owner = THIS_MODULE,
  873. .of_match_table = of_match_ptr(of_pn544_i2c_match),
  874. },
  875. .probe = pn544_hci_i2c_probe,
  876. .id_table = pn544_hci_i2c_id_table,
  877. .remove = pn544_hci_i2c_remove,
  878. };
  879. module_i2c_driver(pn544_hci_i2c_driver);
  880. MODULE_LICENSE("GPL");
  881. MODULE_DESCRIPTION(DRIVER_DESC);