blob: bc0f7ed026056ea145bbf70ca4ad767c68a24921 [file] [log] [blame]
Vinayak Holikattie0eca632013-02-25 21:44:33 +05301/*
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 *
7 * Authors:
8 * Santosh Yaraganavi <santosh.sy@samsung.com>
9 * Vinayak Holikatti <h.vinayak@samsung.com>
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
15 * See the COPYING file in the top-level directory or visit
16 * <http://www.gnu.org/licenses/gpl-2.0.html>
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * This program is provided "AS IS" and "WITH ALL FAULTS" and
24 * without warranty of any kind. You are solely responsible for
25 * determining the appropriateness of using and distributing
26 * the program and assume all risks associated with your exercise
27 * of rights with respect to the program, including but not limited
28 * to infringement of third party rights, the risks and costs of
29 * program errors, damage to or loss of data, programs or equipment,
30 * and unavailability or interruption of operations. Under no
31 * circumstances will the contributor of this Program be liable for
32 * any damages of any kind arising from your use or distribution of
33 * this program.
34 */
35
36#ifndef _UFSHCD_H
37#define _UFSHCD_H
38
39#include <linux/module.h>
40#include <linux/kernel.h>
41#include <linux/init.h>
42#include <linux/interrupt.h>
43#include <linux/io.h>
44#include <linux/delay.h>
45#include <linux/slab.h>
46#include <linux/spinlock.h>
47#include <linux/workqueue.h>
48#include <linux/errno.h>
49#include <linux/types.h>
50#include <linux/wait.h>
51#include <linux/bitops.h>
52#include <linux/pm_runtime.h>
53#include <linux/clk.h>
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +053054#include <linux/completion.h>
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +030055#include <linux/regulator/consumer.h>
Vinayak Holikattie0eca632013-02-25 21:44:33 +053056
57#include <asm/irq.h>
58#include <asm/byteorder.h>
59#include <scsi/scsi.h>
60#include <scsi/scsi_cmnd.h>
61#include <scsi/scsi_host.h>
62#include <scsi/scsi_tcq.h>
63#include <scsi/scsi_dbg.h>
64#include <scsi/scsi_eh.h>
65
66#include "ufs.h"
67#include "ufshci.h"
68
69#define UFSHCD "ufshcd"
70#define UFSHCD_DRIVER_VERSION "0.2"
71
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +030072struct ufs_hba;
73
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +053074enum dev_cmd_type {
75 DEV_CMD_TYPE_NOP = 0x0,
Dolev Raviv68078d52013-07-30 00:35:58 +053076 DEV_CMD_TYPE_QUERY = 0x1,
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +053077};
78
Vinayak Holikattie0eca632013-02-25 21:44:33 +053079/**
80 * struct uic_command - UIC command structure
81 * @command: UIC command
82 * @argument1: UIC command argument 1
83 * @argument2: UIC command argument 2
84 * @argument3: UIC command argument 3
85 * @cmd_active: Indicate if UIC command is outstanding
86 * @result: UIC command result
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +053087 * @done: UIC command completion
Vinayak Holikattie0eca632013-02-25 21:44:33 +053088 */
89struct uic_command {
90 u32 command;
91 u32 argument1;
92 u32 argument2;
93 u32 argument3;
94 int cmd_active;
95 int result;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +053096 struct completion done;
Vinayak Holikattie0eca632013-02-25 21:44:33 +053097};
98
99/**
100 * struct ufshcd_lrb - local reference block
101 * @utr_descriptor_ptr: UTRD address of the command
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530102 * @ucd_req_ptr: UCD address of the command
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530103 * @ucd_rsp_ptr: Response UPIU address for this command
104 * @ucd_prdt_ptr: PRDT address of the command
105 * @cmd: pointer to SCSI command
106 * @sense_buffer: pointer to sense buffer address of the SCSI command
107 * @sense_bufflen: Length of the sense buffer
108 * @scsi_status: SCSI status of the command
109 * @command_type: SCSI, UFS, Query.
110 * @task_tag: Task tag of the command
111 * @lun: LUN of the command
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530112 * @intr_cmd: Interrupt command (doesn't participate in interrupt aggregation)
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530113 */
114struct ufshcd_lrb {
115 struct utp_transfer_req_desc *utr_descriptor_ptr;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530116 struct utp_upiu_req *ucd_req_ptr;
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530117 struct utp_upiu_rsp *ucd_rsp_ptr;
118 struct ufshcd_sg_entry *ucd_prdt_ptr;
119
120 struct scsi_cmnd *cmd;
121 u8 *sense_buffer;
122 unsigned int sense_bufflen;
123 int scsi_status;
124
125 int command_type;
126 int task_tag;
127 unsigned int lun;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530128 bool intr_cmd;
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530129};
130
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530131/**
Dolev Raviv68078d52013-07-30 00:35:58 +0530132 * struct ufs_query - holds relevent data structures for query request
133 * @request: request upiu and function
134 * @descriptor: buffer for sending/receiving descriptor
135 * @response: response upiu and response
136 */
137struct ufs_query {
138 struct ufs_query_req request;
139 u8 *descriptor;
140 struct ufs_query_res response;
141};
142
143/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530144 * struct ufs_dev_cmd - all assosiated fields with device management commands
145 * @type: device management command type - Query, NOP OUT
146 * @lock: lock to allow one command at a time
147 * @complete: internal commands completion
148 * @tag_wq: wait queue until free command slot is available
149 */
150struct ufs_dev_cmd {
151 enum dev_cmd_type type;
152 struct mutex lock;
153 struct completion *complete;
154 wait_queue_head_t tag_wq;
Dolev Raviv68078d52013-07-30 00:35:58 +0530155 struct ufs_query query;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530156};
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530157
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +0300158/**
159 * struct ufs_clk_info - UFS clock related info
160 * @list: list headed by hba->clk_list_head
161 * @clk: clock node
162 * @name: clock name
163 * @max_freq: maximum frequency supported by the clock
164 * @enabled: variable to check against multiple enable/disable
165 */
166struct ufs_clk_info {
167 struct list_head list;
168 struct clk *clk;
169 const char *name;
170 u32 max_freq;
171 bool enabled;
172};
173
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300174#define PRE_CHANGE 0
175#define POST_CHANGE 1
176/**
177 * struct ufs_hba_variant_ops - variant specific callbacks
178 * @name: variant name
179 * @init: called when the driver is initialized
180 * @exit: called to cleanup everything done in init
181 * @setup_clocks: called before touching any of the controller registers
182 * @setup_regulators: called before accessing the host controller
183 * @hce_enable_notify: called before and after HCE enable bit is set to allow
184 * variant specific Uni-Pro initialization.
185 * @link_startup_notify: called before and after Link startup is carried out
186 * to allow variant specific Uni-Pro initialization.
187 */
188struct ufs_hba_variant_ops {
189 const char *name;
190 int (*init)(struct ufs_hba *);
191 void (*exit)(struct ufs_hba *);
192 int (*setup_clocks)(struct ufs_hba *, bool);
193 int (*setup_regulators)(struct ufs_hba *, bool);
194 int (*hce_enable_notify)(struct ufs_hba *, bool);
195 int (*link_startup_notify)(struct ufs_hba *, bool);
196};
197
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530198/**
199 * struct ufs_hba - per adapter private structure
200 * @mmio_base: UFSHCI base register address
201 * @ucdl_base_addr: UFS Command Descriptor base address
202 * @utrdl_base_addr: UTP Transfer Request Descriptor base address
203 * @utmrdl_base_addr: UTP Task Management Descriptor base address
204 * @ucdl_dma_addr: UFS Command Descriptor DMA address
205 * @utrdl_dma_addr: UTRDL DMA address
206 * @utmrdl_dma_addr: UTMRDL DMA address
207 * @host: Scsi_Host instance of the driver
208 * @dev: device handle
209 * @lrb: local reference block
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530210 * @lrb_in_use: lrb in use
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530211 * @outstanding_tasks: Bits representing outstanding task requests
212 * @outstanding_reqs: Bits representing outstanding transfer requests
213 * @capabilities: UFS Controller Capabilities
214 * @nutrs: Transfer Request Queue depth supported by controller
215 * @nutmrs: Task Management Queue depth supported by controller
216 * @ufs_version: UFS Version to which controller complies
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300217 * @vops: pointer to variant specific operations
218 * @priv: pointer to variant specific private data
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530219 * @irq: Irq number of the controller
220 * @active_uic_cmd: handle of active UIC command
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530221 * @uic_cmd_mutex: mutex for uic command
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530222 * @tm_wq: wait queue for task management
223 * @tm_tag_wq: wait queue for free task management slots
224 * @tm_slots_in_use: bit map of task management request slots in use
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +0530225 * @pwr_done: completion for power mode change
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530226 * @tm_condition: condition variable for task management
227 * @ufshcd_state: UFSHCD states
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530228 * @eh_flags: Error handling flags
Seungwon Jeon2fbd0092013-06-26 22:39:27 +0530229 * @intr_mask: Interrupt Mask Bits
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +0530230 * @ee_ctrl_mask: Exception event control mask
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530231 * @eh_work: Worker to handle UFS errors that require s/w attention
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +0530232 * @eeh_work: Worker to handle exception events
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530233 * @errors: HBA errors
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530234 * @uic_error: UFS interconnect layer error status
235 * @saved_err: sticky error mask
236 * @saved_uic_err: sticky UIC error mask
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530237 * @dev_cmd: ufs device management command information
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +0530238 * @auto_bkops_enabled: to track whether bkops is enabled in device
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +0300239 * @vreg_info: UFS device voltage regulator information
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +0300240 * @clk_list_head: UFS host controller clocks list node head
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530241 */
242struct ufs_hba {
243 void __iomem *mmio_base;
244
245 /* Virtual memory reference */
246 struct utp_transfer_cmd_desc *ucdl_base_addr;
247 struct utp_transfer_req_desc *utrdl_base_addr;
248 struct utp_task_req_desc *utmrdl_base_addr;
249
250 /* DMA memory reference */
251 dma_addr_t ucdl_dma_addr;
252 dma_addr_t utrdl_dma_addr;
253 dma_addr_t utmrdl_dma_addr;
254
255 struct Scsi_Host *host;
256 struct device *dev;
257
258 struct ufshcd_lrb *lrb;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530259 unsigned long lrb_in_use;
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530260
261 unsigned long outstanding_tasks;
262 unsigned long outstanding_reqs;
263
264 u32 capabilities;
265 int nutrs;
266 int nutmrs;
267 u32 ufs_version;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300268 struct ufs_hba_variant_ops *vops;
269 void *priv;
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530270 unsigned int irq;
271
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530272 struct uic_command *active_uic_cmd;
273 struct mutex uic_cmd_mutex;
274
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530275 wait_queue_head_t tm_wq;
276 wait_queue_head_t tm_tag_wq;
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530277 unsigned long tm_condition;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530278 unsigned long tm_slots_in_use;
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530279
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +0530280 struct completion *pwr_done;
281
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530282 u32 ufshcd_state;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530283 u32 eh_flags;
Seungwon Jeon2fbd0092013-06-26 22:39:27 +0530284 u32 intr_mask;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +0530285 u16 ee_ctrl_mask;
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530286
287 /* Work Queues */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530288 struct work_struct eh_work;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +0530289 struct work_struct eeh_work;
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530290
291 /* HBA Errors */
292 u32 errors;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530293 u32 uic_error;
294 u32 saved_err;
295 u32 saved_uic_err;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530296
297 /* Device management request data */
298 struct ufs_dev_cmd dev_cmd;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +0530299
300 bool auto_bkops_enabled;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +0300301 struct ufs_vreg_info vreg_info;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +0300302 struct list_head clk_list_head;
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530303};
304
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530305#define ufshcd_writel(hba, val, reg) \
306 writel((val), (hba)->mmio_base + (reg))
307#define ufshcd_readl(hba, reg) \
308 readl((hba)->mmio_base + (reg))
309
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300310int ufshcd_alloc_host(struct device *, struct ufs_hba **);
311int ufshcd_init(struct ufs_hba * , void __iomem * , unsigned int);
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530312void ufshcd_remove(struct ufs_hba *);
313
314/**
315 * ufshcd_hba_stop - Send controller to reset state
316 * @hba: per adapter instance
317 */
318static inline void ufshcd_hba_stop(struct ufs_hba *hba)
319{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530320 ufshcd_writel(hba, CONTROLLER_DISABLE, REG_CONTROLLER_ENABLE);
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530321}
322
Dolev Raviv68078d52013-07-30 00:35:58 +0530323static inline void check_upiu_size(void)
324{
325 BUILD_BUG_ON(ALIGNED_UPIU_SIZE <
326 GENERAL_UPIU_REQUEST_SIZE + QUERY_DESC_MAX_SIZE);
327}
328
Sujit Reddy Thummabdbe5d22014-05-26 10:59:11 +0530329extern int ufshcd_suspend(struct ufs_hba *hba, pm_message_t state);
330extern int ufshcd_resume(struct ufs_hba *hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +0530331extern int ufshcd_runtime_suspend(struct ufs_hba *hba);
332extern int ufshcd_runtime_resume(struct ufs_hba *hba);
333extern int ufshcd_runtime_idle(struct ufs_hba *hba);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +0530334extern int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel,
335 u8 attr_set, u32 mib_val, u8 peer);
336extern int ufshcd_dme_get_attr(struct ufs_hba *hba, u32 attr_sel,
337 u32 *mib_val, u8 peer);
338
339/* UIC command interfaces for DME primitives */
340#define DME_LOCAL 0
341#define DME_PEER 1
342#define ATTR_SET_NOR 0 /* NORMAL */
343#define ATTR_SET_ST 1 /* STATIC */
344
345static inline int ufshcd_dme_set(struct ufs_hba *hba, u32 attr_sel,
346 u32 mib_val)
347{
348 return ufshcd_dme_set_attr(hba, attr_sel, ATTR_SET_NOR,
349 mib_val, DME_LOCAL);
350}
351
352static inline int ufshcd_dme_st_set(struct ufs_hba *hba, u32 attr_sel,
353 u32 mib_val)
354{
355 return ufshcd_dme_set_attr(hba, attr_sel, ATTR_SET_ST,
356 mib_val, DME_LOCAL);
357}
358
359static inline int ufshcd_dme_peer_set(struct ufs_hba *hba, u32 attr_sel,
360 u32 mib_val)
361{
362 return ufshcd_dme_set_attr(hba, attr_sel, ATTR_SET_NOR,
363 mib_val, DME_PEER);
364}
365
366static inline int ufshcd_dme_peer_st_set(struct ufs_hba *hba, u32 attr_sel,
367 u32 mib_val)
368{
369 return ufshcd_dme_set_attr(hba, attr_sel, ATTR_SET_ST,
370 mib_val, DME_PEER);
371}
372
373static inline int ufshcd_dme_get(struct ufs_hba *hba,
374 u32 attr_sel, u32 *mib_val)
375{
376 return ufshcd_dme_get_attr(hba, attr_sel, mib_val, DME_LOCAL);
377}
378
379static inline int ufshcd_dme_peer_get(struct ufs_hba *hba,
380 u32 attr_sel, u32 *mib_val)
381{
382 return ufshcd_dme_get_attr(hba, attr_sel, mib_val, DME_PEER);
383}
384
Vinayak Holikattie0eca632013-02-25 21:44:33 +0530385#endif /* End of Header */