i2c.c 24 KB

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