ufshcd.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  1. /*
  2. * Universal Flash Storage Host controller driver
  3. *
  4. * This code is based on drivers/scsi/ufs/ufshcd.h
  5. * Copyright (C) 2011-2013 Samsung India Software Operations
  6. * Copyright (c) 2013-2016, The Linux Foundation. All rights reserved.
  7. *
  8. * Authors:
  9. * Santosh Yaraganavi <santosh.sy@samsung.com>
  10. * Vinayak Holikatti <h.vinayak@samsung.com>
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version 2
  15. * of the License, or (at your option) any later version.
  16. * See the COPYING file in the top-level directory or visit
  17. * <http://www.gnu.org/licenses/gpl-2.0.html>
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * This program is provided "AS IS" and "WITH ALL FAULTS" and
  25. * without warranty of any kind. You are solely responsible for
  26. * determining the appropriateness of using and distributing
  27. * the program and assume all risks associated with your exercise
  28. * of rights with respect to the program, including but not limited
  29. * to infringement of third party rights, the risks and costs of
  30. * program errors, damage to or loss of data, programs or equipment,
  31. * and unavailability or interruption of operations. Under no
  32. * circumstances will the contributor of this Program be liable for
  33. * any damages of any kind arising from your use or distribution of
  34. * this program.
  35. */
  36. #ifndef _UFSHCD_H
  37. #define _UFSHCD_H
  38. #include <linux/module.h>
  39. #include <linux/kernel.h>
  40. #include <linux/init.h>
  41. #include <linux/interrupt.h>
  42. #include <linux/io.h>
  43. #include <linux/delay.h>
  44. #include <linux/slab.h>
  45. #include <linux/spinlock.h>
  46. #include <linux/workqueue.h>
  47. #include <linux/errno.h>
  48. #include <linux/types.h>
  49. #include <linux/wait.h>
  50. #include <linux/bitops.h>
  51. #include <linux/pm_runtime.h>
  52. #include <linux/clk.h>
  53. #include <linux/completion.h>
  54. #include <linux/regulator/consumer.h>
  55. #include "unipro.h"
  56. #include <asm/irq.h>
  57. #include <asm/byteorder.h>
  58. #include <scsi/scsi.h>
  59. #include <scsi/scsi_cmnd.h>
  60. #include <scsi/scsi_host.h>
  61. #include <scsi/scsi_tcq.h>
  62. #include <scsi/scsi_dbg.h>
  63. #include <scsi/scsi_eh.h>
  64. #include "ufs.h"
  65. #include "ufshci.h"
  66. #define UFSHCD "ufshcd"
  67. #define UFSHCD_DRIVER_VERSION "0.2"
  68. struct ufs_hba;
  69. enum dev_cmd_type {
  70. DEV_CMD_TYPE_NOP = 0x0,
  71. DEV_CMD_TYPE_QUERY = 0x1,
  72. };
  73. /**
  74. * struct uic_command - UIC command structure
  75. * @command: UIC command
  76. * @argument1: UIC command argument 1
  77. * @argument2: UIC command argument 2
  78. * @argument3: UIC command argument 3
  79. * @cmd_active: Indicate if UIC command is outstanding
  80. * @result: UIC command result
  81. * @done: UIC command completion
  82. */
  83. struct uic_command {
  84. u32 command;
  85. u32 argument1;
  86. u32 argument2;
  87. u32 argument3;
  88. int cmd_active;
  89. int result;
  90. struct completion done;
  91. };
  92. /* Used to differentiate the power management options */
  93. enum ufs_pm_op {
  94. UFS_RUNTIME_PM,
  95. UFS_SYSTEM_PM,
  96. UFS_SHUTDOWN_PM,
  97. };
  98. #define ufshcd_is_runtime_pm(op) ((op) == UFS_RUNTIME_PM)
  99. #define ufshcd_is_system_pm(op) ((op) == UFS_SYSTEM_PM)
  100. #define ufshcd_is_shutdown_pm(op) ((op) == UFS_SHUTDOWN_PM)
  101. /* Host <-> Device UniPro Link state */
  102. enum uic_link_state {
  103. UIC_LINK_OFF_STATE = 0, /* Link powered down or disabled */
  104. UIC_LINK_ACTIVE_STATE = 1, /* Link is in Fast/Slow/Sleep state */
  105. UIC_LINK_HIBERN8_STATE = 2, /* Link is in Hibernate state */
  106. };
  107. #define ufshcd_is_link_off(hba) ((hba)->uic_link_state == UIC_LINK_OFF_STATE)
  108. #define ufshcd_is_link_active(hba) ((hba)->uic_link_state == \
  109. UIC_LINK_ACTIVE_STATE)
  110. #define ufshcd_is_link_hibern8(hba) ((hba)->uic_link_state == \
  111. UIC_LINK_HIBERN8_STATE)
  112. #define ufshcd_set_link_off(hba) ((hba)->uic_link_state = UIC_LINK_OFF_STATE)
  113. #define ufshcd_set_link_active(hba) ((hba)->uic_link_state = \
  114. UIC_LINK_ACTIVE_STATE)
  115. #define ufshcd_set_link_hibern8(hba) ((hba)->uic_link_state = \
  116. UIC_LINK_HIBERN8_STATE)
  117. /*
  118. * UFS Power management levels.
  119. * Each level is in increasing order of power savings.
  120. */
  121. enum ufs_pm_level {
  122. UFS_PM_LVL_0, /* UFS_ACTIVE_PWR_MODE, UIC_LINK_ACTIVE_STATE */
  123. UFS_PM_LVL_1, /* UFS_ACTIVE_PWR_MODE, UIC_LINK_HIBERN8_STATE */
  124. UFS_PM_LVL_2, /* UFS_SLEEP_PWR_MODE, UIC_LINK_ACTIVE_STATE */
  125. UFS_PM_LVL_3, /* UFS_SLEEP_PWR_MODE, UIC_LINK_HIBERN8_STATE */
  126. UFS_PM_LVL_4, /* UFS_POWERDOWN_PWR_MODE, UIC_LINK_HIBERN8_STATE */
  127. UFS_PM_LVL_5, /* UFS_POWERDOWN_PWR_MODE, UIC_LINK_OFF_STATE */
  128. UFS_PM_LVL_MAX
  129. };
  130. struct ufs_pm_lvl_states {
  131. enum ufs_dev_pwr_mode dev_state;
  132. enum uic_link_state link_state;
  133. };
  134. /**
  135. * struct ufshcd_lrb - local reference block
  136. * @utr_descriptor_ptr: UTRD address of the command
  137. * @ucd_req_ptr: UCD address of the command
  138. * @ucd_rsp_ptr: Response UPIU address for this command
  139. * @ucd_prdt_ptr: PRDT address of the command
  140. * @cmd: pointer to SCSI command
  141. * @sense_buffer: pointer to sense buffer address of the SCSI command
  142. * @sense_bufflen: Length of the sense buffer
  143. * @scsi_status: SCSI status of the command
  144. * @command_type: SCSI, UFS, Query.
  145. * @task_tag: Task tag of the command
  146. * @lun: LUN of the command
  147. * @intr_cmd: Interrupt command (doesn't participate in interrupt aggregation)
  148. */
  149. struct ufshcd_lrb {
  150. struct utp_transfer_req_desc *utr_descriptor_ptr;
  151. struct utp_upiu_req *ucd_req_ptr;
  152. struct utp_upiu_rsp *ucd_rsp_ptr;
  153. struct ufshcd_sg_entry *ucd_prdt_ptr;
  154. struct scsi_cmnd *cmd;
  155. u8 *sense_buffer;
  156. unsigned int sense_bufflen;
  157. int scsi_status;
  158. int command_type;
  159. int task_tag;
  160. u8 lun; /* UPIU LUN id field is only 8-bit wide */
  161. bool intr_cmd;
  162. };
  163. /**
  164. * struct ufs_query - holds relevant data structures for query request
  165. * @request: request upiu and function
  166. * @descriptor: buffer for sending/receiving descriptor
  167. * @response: response upiu and response
  168. */
  169. struct ufs_query {
  170. struct ufs_query_req request;
  171. u8 *descriptor;
  172. struct ufs_query_res response;
  173. };
  174. /**
  175. * struct ufs_dev_cmd - all assosiated fields with device management commands
  176. * @type: device management command type - Query, NOP OUT
  177. * @lock: lock to allow one command at a time
  178. * @complete: internal commands completion
  179. * @tag_wq: wait queue until free command slot is available
  180. */
  181. struct ufs_dev_cmd {
  182. enum dev_cmd_type type;
  183. struct mutex lock;
  184. struct completion *complete;
  185. wait_queue_head_t tag_wq;
  186. struct ufs_query query;
  187. };
  188. struct ufs_desc_size {
  189. int dev_desc;
  190. int pwr_desc;
  191. int geom_desc;
  192. int interc_desc;
  193. int unit_desc;
  194. int conf_desc;
  195. };
  196. /**
  197. * struct ufs_clk_info - UFS clock related info
  198. * @list: list headed by hba->clk_list_head
  199. * @clk: clock node
  200. * @name: clock name
  201. * @max_freq: maximum frequency supported by the clock
  202. * @min_freq: min frequency that can be used for clock scaling
  203. * @curr_freq: indicates the current frequency that it is set to
  204. * @enabled: variable to check against multiple enable/disable
  205. */
  206. struct ufs_clk_info {
  207. struct list_head list;
  208. struct clk *clk;
  209. const char *name;
  210. u32 max_freq;
  211. u32 min_freq;
  212. u32 curr_freq;
  213. bool enabled;
  214. };
  215. enum ufs_notify_change_status {
  216. PRE_CHANGE,
  217. POST_CHANGE,
  218. };
  219. struct ufs_pa_layer_attr {
  220. u32 gear_rx;
  221. u32 gear_tx;
  222. u32 lane_rx;
  223. u32 lane_tx;
  224. u32 pwr_rx;
  225. u32 pwr_tx;
  226. u32 hs_rate;
  227. };
  228. struct ufs_pwr_mode_info {
  229. bool is_valid;
  230. struct ufs_pa_layer_attr info;
  231. };
  232. /**
  233. * struct ufs_hba_variant_ops - variant specific callbacks
  234. * @name: variant name
  235. * @init: called when the driver is initialized
  236. * @exit: called to cleanup everything done in init
  237. * @get_ufs_hci_version: called to get UFS HCI version
  238. * @clk_scale_notify: notifies that clks are scaled up/down
  239. * @setup_clocks: called before touching any of the controller registers
  240. * @setup_regulators: called before accessing the host controller
  241. * @hce_enable_notify: called before and after HCE enable bit is set to allow
  242. * variant specific Uni-Pro initialization.
  243. * @link_startup_notify: called before and after Link startup is carried out
  244. * to allow variant specific Uni-Pro initialization.
  245. * @pwr_change_notify: called before and after a power mode change
  246. * is carried out to allow vendor spesific capabilities
  247. * to be set.
  248. * @apply_dev_quirks: called to apply device specific quirks
  249. * @suspend: called during host controller PM callback
  250. * @resume: called during host controller PM callback
  251. * @dbg_register_dump: used to dump controller debug information
  252. * @phy_initialization: used to initialize phys
  253. */
  254. struct ufs_hba_variant_ops {
  255. const char *name;
  256. int (*init)(struct ufs_hba *);
  257. void (*exit)(struct ufs_hba *);
  258. u32 (*get_ufs_hci_version)(struct ufs_hba *);
  259. int (*clk_scale_notify)(struct ufs_hba *, bool,
  260. enum ufs_notify_change_status);
  261. int (*setup_clocks)(struct ufs_hba *, bool);
  262. int (*setup_regulators)(struct ufs_hba *, bool);
  263. int (*hce_enable_notify)(struct ufs_hba *,
  264. enum ufs_notify_change_status);
  265. int (*link_startup_notify)(struct ufs_hba *,
  266. enum ufs_notify_change_status);
  267. int (*pwr_change_notify)(struct ufs_hba *,
  268. enum ufs_notify_change_status status,
  269. struct ufs_pa_layer_attr *,
  270. struct ufs_pa_layer_attr *);
  271. int (*apply_dev_quirks)(struct ufs_hba *);
  272. int (*suspend)(struct ufs_hba *, enum ufs_pm_op);
  273. int (*resume)(struct ufs_hba *, enum ufs_pm_op);
  274. void (*dbg_register_dump)(struct ufs_hba *hba);
  275. int (*phy_initialization)(struct ufs_hba *);
  276. };
  277. /* clock gating state */
  278. enum clk_gating_state {
  279. CLKS_OFF,
  280. CLKS_ON,
  281. REQ_CLKS_OFF,
  282. REQ_CLKS_ON,
  283. };
  284. /**
  285. * struct ufs_clk_gating - UFS clock gating related info
  286. * @gate_work: worker to turn off clocks after some delay as specified in
  287. * delay_ms
  288. * @ungate_work: worker to turn on clocks that will be used in case of
  289. * interrupt context
  290. * @state: the current clocks state
  291. * @delay_ms: gating delay in ms
  292. * @is_suspended: clk gating is suspended when set to 1 which can be used
  293. * during suspend/resume
  294. * @delay_attr: sysfs attribute to control delay_attr
  295. * @active_reqs: number of requests that are pending and should be waited for
  296. * completion before gating clocks.
  297. */
  298. struct ufs_clk_gating {
  299. struct delayed_work gate_work;
  300. struct work_struct ungate_work;
  301. enum clk_gating_state state;
  302. unsigned long delay_ms;
  303. bool is_suspended;
  304. struct device_attribute delay_attr;
  305. int active_reqs;
  306. };
  307. struct ufs_clk_scaling {
  308. ktime_t busy_start_t;
  309. bool is_busy_started;
  310. unsigned long tot_busy_t;
  311. unsigned long window_start_t;
  312. };
  313. /**
  314. * struct ufs_init_prefetch - contains data that is pre-fetched once during
  315. * initialization
  316. * @icc_level: icc level which was read during initialization
  317. */
  318. struct ufs_init_prefetch {
  319. u32 icc_level;
  320. };
  321. /**
  322. * struct ufs_hba - per adapter private structure
  323. * @mmio_base: UFSHCI base register address
  324. * @ucdl_base_addr: UFS Command Descriptor base address
  325. * @utrdl_base_addr: UTP Transfer Request Descriptor base address
  326. * @utmrdl_base_addr: UTP Task Management Descriptor base address
  327. * @ucdl_dma_addr: UFS Command Descriptor DMA address
  328. * @utrdl_dma_addr: UTRDL DMA address
  329. * @utmrdl_dma_addr: UTMRDL DMA address
  330. * @host: Scsi_Host instance of the driver
  331. * @dev: device handle
  332. * @lrb: local reference block
  333. * @lrb_in_use: lrb in use
  334. * @outstanding_tasks: Bits representing outstanding task requests
  335. * @outstanding_reqs: Bits representing outstanding transfer requests
  336. * @capabilities: UFS Controller Capabilities
  337. * @nutrs: Transfer Request Queue depth supported by controller
  338. * @nutmrs: Task Management Queue depth supported by controller
  339. * @ufs_version: UFS Version to which controller complies
  340. * @vops: pointer to variant specific operations
  341. * @priv: pointer to variant specific private data
  342. * @irq: Irq number of the controller
  343. * @active_uic_cmd: handle of active UIC command
  344. * @uic_cmd_mutex: mutex for uic command
  345. * @tm_wq: wait queue for task management
  346. * @tm_tag_wq: wait queue for free task management slots
  347. * @tm_slots_in_use: bit map of task management request slots in use
  348. * @pwr_done: completion for power mode change
  349. * @tm_condition: condition variable for task management
  350. * @ufshcd_state: UFSHCD states
  351. * @eh_flags: Error handling flags
  352. * @intr_mask: Interrupt Mask Bits
  353. * @ee_ctrl_mask: Exception event control mask
  354. * @is_powered: flag to check if HBA is powered
  355. * @is_init_prefetch: flag to check if data was pre-fetched in initialization
  356. * @init_prefetch_data: data pre-fetched during initialization
  357. * @eh_work: Worker to handle UFS errors that require s/w attention
  358. * @eeh_work: Worker to handle exception events
  359. * @errors: HBA errors
  360. * @uic_error: UFS interconnect layer error status
  361. * @saved_err: sticky error mask
  362. * @saved_uic_err: sticky UIC error mask
  363. * @dev_cmd: ufs device management command information
  364. * @last_dme_cmd_tstamp: time stamp of the last completed DME command
  365. * @auto_bkops_enabled: to track whether bkops is enabled in device
  366. * @vreg_info: UFS device voltage regulator information
  367. * @clk_list_head: UFS host controller clocks list node head
  368. * @pwr_info: holds current power mode
  369. * @max_pwr_info: keeps the device max valid pwm
  370. * @desc_size: descriptor sizes reported by device
  371. * @urgent_bkops_lvl: keeps track of urgent bkops level for device
  372. * @is_urgent_bkops_lvl_checked: keeps track if the urgent bkops level for
  373. * device is known or not.
  374. */
  375. struct ufs_hba {
  376. void __iomem *mmio_base;
  377. /* Virtual memory reference */
  378. struct utp_transfer_cmd_desc *ucdl_base_addr;
  379. struct utp_transfer_req_desc *utrdl_base_addr;
  380. struct utp_task_req_desc *utmrdl_base_addr;
  381. /* DMA memory reference */
  382. dma_addr_t ucdl_dma_addr;
  383. dma_addr_t utrdl_dma_addr;
  384. dma_addr_t utmrdl_dma_addr;
  385. struct Scsi_Host *host;
  386. struct device *dev;
  387. /*
  388. * This field is to keep a reference to "scsi_device" corresponding to
  389. * "UFS device" W-LU.
  390. */
  391. struct scsi_device *sdev_ufs_device;
  392. enum ufs_dev_pwr_mode curr_dev_pwr_mode;
  393. enum uic_link_state uic_link_state;
  394. /* Desired UFS power management level during runtime PM */
  395. enum ufs_pm_level rpm_lvl;
  396. /* Desired UFS power management level during system PM */
  397. enum ufs_pm_level spm_lvl;
  398. int pm_op_in_progress;
  399. struct ufshcd_lrb *lrb;
  400. unsigned long lrb_in_use;
  401. unsigned long outstanding_tasks;
  402. unsigned long outstanding_reqs;
  403. u32 capabilities;
  404. int nutrs;
  405. int nutmrs;
  406. u32 ufs_version;
  407. struct ufs_hba_variant_ops *vops;
  408. void *priv;
  409. unsigned int irq;
  410. bool is_irq_enabled;
  411. /* Interrupt aggregation support is broken */
  412. #define UFSHCD_QUIRK_BROKEN_INTR_AGGR UFS_BIT(0)
  413. /*
  414. * delay before each dme command is required as the unipro
  415. * layer has shown instabilities
  416. */
  417. #define UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS UFS_BIT(1)
  418. /*
  419. * If UFS host controller is having issue in processing LCC (Line
  420. * Control Command) coming from device then enable this quirk.
  421. * When this quirk is enabled, host controller driver should disable
  422. * the LCC transmission on UFS device (by clearing TX_LCC_ENABLE
  423. * attribute of device to 0).
  424. */
  425. #define UFSHCD_QUIRK_BROKEN_LCC UFS_BIT(2)
  426. /*
  427. * The attribute PA_RXHSUNTERMCAP specifies whether or not the
  428. * inbound Link supports unterminated line in HS mode. Setting this
  429. * attribute to 1 fixes moving to HS gear.
  430. */
  431. #define UFSHCD_QUIRK_BROKEN_PA_RXHSUNTERMCAP UFS_BIT(3)
  432. /*
  433. * This quirk needs to be enabled if the host contoller only allows
  434. * accessing the peer dme attributes in AUTO mode (FAST AUTO or
  435. * SLOW AUTO).
  436. */
  437. #define UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE UFS_BIT(4)
  438. /*
  439. * This quirk needs to be enabled if the host contoller doesn't
  440. * advertise the correct version in UFS_VER register. If this quirk
  441. * is enabled, standard UFS host driver will call the vendor specific
  442. * ops (get_ufs_hci_version) to get the correct version.
  443. */
  444. #define UFSHCD_QUIRK_BROKEN_UFS_HCI_VERSION UFS_BIT(5)
  445. /*
  446. * This quirk needs to be enabled if the host contoller regards
  447. * resolution of the values of PRDTO and PRDTL in UTRD as byte.
  448. */
  449. #define UFSHCD_QUIRK_PRDT_BYTE_GRAN UFS_BIT(7)
  450. unsigned int quirks; /* Deviations from standard UFSHCI spec. */
  451. /* Device deviations from standard UFS device spec. */
  452. unsigned int dev_quirks;
  453. wait_queue_head_t tm_wq;
  454. wait_queue_head_t tm_tag_wq;
  455. unsigned long tm_condition;
  456. unsigned long tm_slots_in_use;
  457. struct uic_command *active_uic_cmd;
  458. struct mutex uic_cmd_mutex;
  459. struct completion *uic_async_done;
  460. u32 ufshcd_state;
  461. u32 eh_flags;
  462. u32 intr_mask;
  463. u16 ee_ctrl_mask;
  464. bool is_powered;
  465. bool is_init_prefetch;
  466. struct ufs_init_prefetch init_prefetch_data;
  467. /* Work Queues */
  468. struct work_struct eh_work;
  469. struct work_struct eeh_work;
  470. /* HBA Errors */
  471. u32 errors;
  472. u32 uic_error;
  473. u32 saved_err;
  474. u32 saved_uic_err;
  475. /* Device management request data */
  476. struct ufs_dev_cmd dev_cmd;
  477. ktime_t last_dme_cmd_tstamp;
  478. /* Keeps information of the UFS device connected to this host */
  479. struct ufs_dev_info dev_info;
  480. bool auto_bkops_enabled;
  481. struct ufs_vreg_info vreg_info;
  482. struct list_head clk_list_head;
  483. bool wlun_dev_clr_ua;
  484. /* Number of lanes available (1 or 2) for Rx/Tx */
  485. u32 lanes_per_direction;
  486. struct ufs_pa_layer_attr pwr_info;
  487. struct ufs_pwr_mode_info max_pwr_info;
  488. struct ufs_clk_gating clk_gating;
  489. /* Control to enable/disable host capabilities */
  490. u32 caps;
  491. /* Allow dynamic clk gating */
  492. #define UFSHCD_CAP_CLK_GATING (1 << 0)
  493. /* Allow hiberb8 with clk gating */
  494. #define UFSHCD_CAP_HIBERN8_WITH_CLK_GATING (1 << 1)
  495. /* Allow dynamic clk scaling */
  496. #define UFSHCD_CAP_CLK_SCALING (1 << 2)
  497. /* Allow auto bkops to enabled during runtime suspend */
  498. #define UFSHCD_CAP_AUTO_BKOPS_SUSPEND (1 << 3)
  499. /*
  500. * This capability allows host controller driver to use the UFS HCI's
  501. * interrupt aggregation capability.
  502. * CAUTION: Enabling this might reduce overall UFS throughput.
  503. */
  504. #define UFSHCD_CAP_INTR_AGGR (1 << 4)
  505. /*
  506. * This capability allows the device auto-bkops to be always enabled
  507. * except during suspend (both runtime and suspend).
  508. * Enabling this capability means that device will always be allowed
  509. * to do background operation when it's active but it might degrade
  510. * the performance of ongoing read/write operations.
  511. */
  512. #define UFSHCD_CAP_KEEP_AUTO_BKOPS_ENABLED_EXCEPT_SUSPEND (1 << 5)
  513. struct devfreq *devfreq;
  514. struct ufs_clk_scaling clk_scaling;
  515. bool is_sys_suspended;
  516. enum bkops_status urgent_bkops_lvl;
  517. bool is_urgent_bkops_lvl_checked;
  518. struct ufs_desc_size desc_size;
  519. };
  520. /* Returns true if clocks can be gated. Otherwise false */
  521. static inline bool ufshcd_is_clkgating_allowed(struct ufs_hba *hba)
  522. {
  523. return hba->caps & UFSHCD_CAP_CLK_GATING;
  524. }
  525. static inline bool ufshcd_can_hibern8_during_gating(struct ufs_hba *hba)
  526. {
  527. return hba->caps & UFSHCD_CAP_HIBERN8_WITH_CLK_GATING;
  528. }
  529. static inline int ufshcd_is_clkscaling_enabled(struct ufs_hba *hba)
  530. {
  531. return hba->caps & UFSHCD_CAP_CLK_SCALING;
  532. }
  533. static inline bool ufshcd_can_autobkops_during_suspend(struct ufs_hba *hba)
  534. {
  535. return hba->caps & UFSHCD_CAP_AUTO_BKOPS_SUSPEND;
  536. }
  537. static inline bool ufshcd_is_intr_aggr_allowed(struct ufs_hba *hba)
  538. {
  539. /* DWC UFS Core has the Interrupt aggregation feature but is not detectable*/
  540. #ifndef CONFIG_SCSI_UFS_DWC
  541. if ((hba->caps & UFSHCD_CAP_INTR_AGGR) &&
  542. !(hba->quirks & UFSHCD_QUIRK_BROKEN_INTR_AGGR))
  543. return true;
  544. else
  545. return false;
  546. #else
  547. return true;
  548. #endif
  549. }
  550. #define ufshcd_writel(hba, val, reg) \
  551. writel((val), (hba)->mmio_base + (reg))
  552. #define ufshcd_readl(hba, reg) \
  553. readl((hba)->mmio_base + (reg))
  554. /**
  555. * ufshcd_rmwl - read modify write into a register
  556. * @hba - per adapter instance
  557. * @mask - mask to apply on read value
  558. * @val - actual value to write
  559. * @reg - register address
  560. */
  561. static inline void ufshcd_rmwl(struct ufs_hba *hba, u32 mask, u32 val, u32 reg)
  562. {
  563. u32 tmp;
  564. tmp = ufshcd_readl(hba, reg);
  565. tmp &= ~mask;
  566. tmp |= (val & mask);
  567. ufshcd_writel(hba, tmp, reg);
  568. }
  569. int ufshcd_alloc_host(struct device *, struct ufs_hba **);
  570. void ufshcd_dealloc_host(struct ufs_hba *);
  571. int ufshcd_init(struct ufs_hba * , void __iomem * , unsigned int);
  572. void ufshcd_remove(struct ufs_hba *);
  573. int ufshcd_wait_for_register(struct ufs_hba *hba, u32 reg, u32 mask,
  574. u32 val, unsigned long interval_us,
  575. unsigned long timeout_ms, bool can_sleep);
  576. static inline void check_upiu_size(void)
  577. {
  578. BUILD_BUG_ON(ALIGNED_UPIU_SIZE <
  579. GENERAL_UPIU_REQUEST_SIZE + QUERY_DESC_MAX_SIZE);
  580. }
  581. /**
  582. * ufshcd_set_variant - set variant specific data to the hba
  583. * @hba - per adapter instance
  584. * @variant - pointer to variant specific data
  585. */
  586. static inline void ufshcd_set_variant(struct ufs_hba *hba, void *variant)
  587. {
  588. BUG_ON(!hba);
  589. hba->priv = variant;
  590. }
  591. /**
  592. * ufshcd_get_variant - get variant specific data from the hba
  593. * @hba - per adapter instance
  594. */
  595. static inline void *ufshcd_get_variant(struct ufs_hba *hba)
  596. {
  597. BUG_ON(!hba);
  598. return hba->priv;
  599. }
  600. static inline bool ufshcd_keep_autobkops_enabled_except_suspend(
  601. struct ufs_hba *hba)
  602. {
  603. return hba->caps & UFSHCD_CAP_KEEP_AUTO_BKOPS_ENABLED_EXCEPT_SUSPEND;
  604. }
  605. extern int ufshcd_runtime_suspend(struct ufs_hba *hba);
  606. extern int ufshcd_runtime_resume(struct ufs_hba *hba);
  607. extern int ufshcd_runtime_idle(struct ufs_hba *hba);
  608. extern int ufshcd_system_suspend(struct ufs_hba *hba);
  609. extern int ufshcd_system_resume(struct ufs_hba *hba);
  610. extern int ufshcd_shutdown(struct ufs_hba *hba);
  611. extern int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel,
  612. u8 attr_set, u32 mib_val, u8 peer);
  613. extern int ufshcd_dme_get_attr(struct ufs_hba *hba, u32 attr_sel,
  614. u32 *mib_val, u8 peer);
  615. /* UIC command interfaces for DME primitives */
  616. #define DME_LOCAL 0
  617. #define DME_PEER 1
  618. #define ATTR_SET_NOR 0 /* NORMAL */
  619. #define ATTR_SET_ST 1 /* STATIC */
  620. static inline int ufshcd_dme_set(struct ufs_hba *hba, u32 attr_sel,
  621. u32 mib_val)
  622. {
  623. return ufshcd_dme_set_attr(hba, attr_sel, ATTR_SET_NOR,
  624. mib_val, DME_LOCAL);
  625. }
  626. static inline int ufshcd_dme_st_set(struct ufs_hba *hba, u32 attr_sel,
  627. u32 mib_val)
  628. {
  629. return ufshcd_dme_set_attr(hba, attr_sel, ATTR_SET_ST,
  630. mib_val, DME_LOCAL);
  631. }
  632. static inline int ufshcd_dme_peer_set(struct ufs_hba *hba, u32 attr_sel,
  633. u32 mib_val)
  634. {
  635. return ufshcd_dme_set_attr(hba, attr_sel, ATTR_SET_NOR,
  636. mib_val, DME_PEER);
  637. }
  638. static inline int ufshcd_dme_peer_st_set(struct ufs_hba *hba, u32 attr_sel,
  639. u32 mib_val)
  640. {
  641. return ufshcd_dme_set_attr(hba, attr_sel, ATTR_SET_ST,
  642. mib_val, DME_PEER);
  643. }
  644. static inline int ufshcd_dme_get(struct ufs_hba *hba,
  645. u32 attr_sel, u32 *mib_val)
  646. {
  647. return ufshcd_dme_get_attr(hba, attr_sel, mib_val, DME_LOCAL);
  648. }
  649. static inline int ufshcd_dme_peer_get(struct ufs_hba *hba,
  650. u32 attr_sel, u32 *mib_val)
  651. {
  652. return ufshcd_dme_get_attr(hba, attr_sel, mib_val, DME_PEER);
  653. }
  654. int ufshcd_read_device_desc(struct ufs_hba *hba, u8 *buf, u32 size);
  655. static inline bool ufshcd_is_hs_mode(struct ufs_pa_layer_attr *pwr_info)
  656. {
  657. return (pwr_info->pwr_rx == FAST_MODE ||
  658. pwr_info->pwr_rx == FASTAUTO_MODE) &&
  659. (pwr_info->pwr_tx == FAST_MODE ||
  660. pwr_info->pwr_tx == FASTAUTO_MODE);
  661. }
  662. #define ASCII_STD true
  663. int ufshcd_read_string_desc(struct ufs_hba *hba, int desc_index, u8 *buf,
  664. u32 size, bool ascii);
  665. /* Expose Query-Request API */
  666. int ufshcd_query_flag(struct ufs_hba *hba, enum query_opcode opcode,
  667. enum flag_idn idn, bool *flag_res);
  668. int ufshcd_hold(struct ufs_hba *hba, bool async);
  669. void ufshcd_release(struct ufs_hba *hba);
  670. int ufshcd_map_desc_id_to_length(struct ufs_hba *hba, enum desc_idn desc_id,
  671. int *desc_length);
  672. u32 ufshcd_get_local_unipro_ver(struct ufs_hba *hba);
  673. /* Wrapper functions for safely calling variant operations */
  674. static inline const char *ufshcd_get_var_name(struct ufs_hba *hba)
  675. {
  676. if (hba->vops)
  677. return hba->vops->name;
  678. return "";
  679. }
  680. static inline int ufshcd_vops_init(struct ufs_hba *hba)
  681. {
  682. if (hba->vops && hba->vops->init)
  683. return hba->vops->init(hba);
  684. return 0;
  685. }
  686. static inline void ufshcd_vops_exit(struct ufs_hba *hba)
  687. {
  688. if (hba->vops && hba->vops->exit)
  689. return hba->vops->exit(hba);
  690. }
  691. static inline u32 ufshcd_vops_get_ufs_hci_version(struct ufs_hba *hba)
  692. {
  693. if (hba->vops && hba->vops->get_ufs_hci_version)
  694. return hba->vops->get_ufs_hci_version(hba);
  695. return ufshcd_readl(hba, REG_UFS_VERSION);
  696. }
  697. static inline int ufshcd_vops_clk_scale_notify(struct ufs_hba *hba,
  698. bool up, enum ufs_notify_change_status status)
  699. {
  700. if (hba->vops && hba->vops->clk_scale_notify)
  701. return hba->vops->clk_scale_notify(hba, up, status);
  702. return 0;
  703. }
  704. static inline int ufshcd_vops_setup_clocks(struct ufs_hba *hba, bool on)
  705. {
  706. if (hba->vops && hba->vops->setup_clocks)
  707. return hba->vops->setup_clocks(hba, on);
  708. return 0;
  709. }
  710. static inline int ufshcd_vops_setup_regulators(struct ufs_hba *hba, bool status)
  711. {
  712. if (hba->vops && hba->vops->setup_regulators)
  713. return hba->vops->setup_regulators(hba, status);
  714. return 0;
  715. }
  716. static inline int ufshcd_vops_hce_enable_notify(struct ufs_hba *hba,
  717. bool status)
  718. {
  719. if (hba->vops && hba->vops->hce_enable_notify)
  720. return hba->vops->hce_enable_notify(hba, status);
  721. return 0;
  722. }
  723. static inline int ufshcd_vops_link_startup_notify(struct ufs_hba *hba,
  724. bool status)
  725. {
  726. if (hba->vops && hba->vops->link_startup_notify)
  727. return hba->vops->link_startup_notify(hba, status);
  728. return 0;
  729. }
  730. static inline int ufshcd_vops_pwr_change_notify(struct ufs_hba *hba,
  731. bool status,
  732. struct ufs_pa_layer_attr *dev_max_params,
  733. struct ufs_pa_layer_attr *dev_req_params)
  734. {
  735. if (hba->vops && hba->vops->pwr_change_notify)
  736. return hba->vops->pwr_change_notify(hba, status,
  737. dev_max_params, dev_req_params);
  738. return -ENOTSUPP;
  739. }
  740. static inline int ufshcd_vops_apply_dev_quirks(struct ufs_hba *hba)
  741. {
  742. if (hba->vops && hba->vops->apply_dev_quirks)
  743. return hba->vops->apply_dev_quirks(hba);
  744. return 0;
  745. }
  746. static inline int ufshcd_vops_suspend(struct ufs_hba *hba, enum ufs_pm_op op)
  747. {
  748. if (hba->vops && hba->vops->suspend)
  749. return hba->vops->suspend(hba, op);
  750. return 0;
  751. }
  752. static inline int ufshcd_vops_resume(struct ufs_hba *hba, enum ufs_pm_op op)
  753. {
  754. if (hba->vops && hba->vops->resume)
  755. return hba->vops->resume(hba, op);
  756. return 0;
  757. }
  758. static inline void ufshcd_vops_dbg_register_dump(struct ufs_hba *hba)
  759. {
  760. if (hba->vops && hba->vops->dbg_register_dump)
  761. hba->vops->dbg_register_dump(hba);
  762. }
  763. #endif /* End of Header */