blob: 3b226adb08b5d27d2a1369d21288c017bc32e290 [file] [log] [blame]
Hamad Kadmany090709b2013-01-06 12:08:13 +02001/* Copyright (c) 2011-2013, The Linux Foundation. All rights reserved.
Joel Nider5556a852011-10-16 10:52:13 +02002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
Joel Nider435ad8e2011-12-14 16:53:30 +020013#include <linux/module.h> /* Just for modules */
14#include <linux/kernel.h> /* Only for KERN_INFO */
15#include <linux/err.h> /* Error macros */
16#include <linux/list.h> /* Linked list */
Joel Nider5556a852011-10-16 10:52:13 +020017#include <linux/cdev.h>
Joel Nider435ad8e2011-12-14 16:53:30 +020018#include <linux/init.h> /* Needed for the macros */
19#include <linux/io.h> /* IO macros */
20#include <linux/device.h> /* Device drivers need this */
21#include <linux/sched.h> /* Externally defined globals */
22#include <linux/pm_runtime.h> /* Runtime power management */
Joel Nider5556a852011-10-16 10:52:13 +020023#include <linux/fs.h>
Joel Nider435ad8e2011-12-14 16:53:30 +020024#include <linux/uaccess.h> /* copy_to_user */
Joel Nider5556a852011-10-16 10:52:13 +020025#include <linux/slab.h> /* kfree, kzalloc */
Joel Nider435ad8e2011-12-14 16:53:30 +020026#include <linux/ioport.h> /* XXX_ mem_region */
27#include <linux/dma-mapping.h> /* dma_XXX */
Hamad Kadmany090709b2013-01-06 12:08:13 +020028#include <linux/dmapool.h> /* DMA pools */
Joel Nider435ad8e2011-12-14 16:53:30 +020029#include <linux/delay.h> /* msleep */
30#include <linux/platform_device.h>
Joel Nider5556a852011-10-16 10:52:13 +020031#include <linux/clk.h>
Joel Nider435ad8e2011-12-14 16:53:30 +020032#include <linux/poll.h> /* poll() file op */
33#include <linux/wait.h> /* wait() macros, sleeping */
34#include <linux/tspp.h> /* tspp functions */
Joel Nider5556a852011-10-16 10:52:13 +020035#include <linux/bitops.h> /* BIT() macro */
Joel Nider435ad8e2011-12-14 16:53:30 +020036#include <mach/sps.h> /* BAM stuff */
Joel Nider5556a852011-10-16 10:52:13 +020037#include <mach/gpio.h>
Joel Nider435ad8e2011-12-14 16:53:30 +020038#include <linux/wakelock.h> /* Locking functions */
Hamad Kadmany81cee052012-11-29 14:15:57 +020039#include <linux/timer.h> /* Timer services */
40#include <linux/jiffies.h> /* Jiffies counter */
Joel Nider5556a852011-10-16 10:52:13 +020041#include <mach/dma.h>
42#include <mach/msm_tspp.h>
Joel Nider5556a852011-10-16 10:52:13 +020043#include <linux/debugfs.h>
Liron Kuch59339922013-01-01 18:29:47 +020044#include <linux/of.h>
45#include <linux/of_gpio.h>
Liron Kuch275c0b32013-02-10 15:19:32 +020046#include <linux/string.h>
Joel Nider5556a852011-10-16 10:52:13 +020047
48/*
49 * General defines
50 */
Joel Nider5556a852011-10-16 10:52:13 +020051#define TSPP_TSIF_INSTANCES 2
Liron Kuch275c0b32013-02-10 15:19:32 +020052#define TSPP_GPIOS_PER_TSIF 4
Joel Nider5556a852011-10-16 10:52:13 +020053#define TSPP_FILTER_TABLES 3
Joel Nider435ad8e2011-12-14 16:53:30 +020054#define TSPP_MAX_DEVICES 1
Joel Nider5556a852011-10-16 10:52:13 +020055#define TSPP_NUM_CHANNELS 16
56#define TSPP_NUM_PRIORITIES 16
57#define TSPP_NUM_KEYS 8
58#define INVALID_CHANNEL 0xFFFFFFFF
Hamad Kadmany81cee052012-11-29 14:15:57 +020059
60/*
61 * BAM descriptor FIFO size (in number of descriptors).
62 * Max number of descriptors allowed by SPS which is 8K-1.
63 * Restrict it to half of this to save DMA memory.
64 */
65#define TSPP_SPS_DESCRIPTOR_COUNT (4 * 1024 - 1)
Joel Nider5556a852011-10-16 10:52:13 +020066#define TSPP_PACKET_LENGTH 188
67#define TSPP_MIN_BUFFER_SIZE (TSPP_PACKET_LENGTH)
Hamad Kadmany81cee052012-11-29 14:15:57 +020068
69/* Max descriptor buffer size allowed by SPS */
70#define TSPP_MAX_BUFFER_SIZE (32 * 1024 - 1)
71
72/*
Hamad Kadmany090709b2013-01-06 12:08:13 +020073 * Returns whether to use DMA pool for TSPP output buffers.
74 * For buffers smaller than page size, using DMA pool
75 * provides better memory utilization as dma_alloc_coherent
76 * allocates minimum of page size.
77 */
78#define TSPP_USE_DMA_POOL(buff_size) ((buff_size) < PAGE_SIZE)
79
80/*
Hamad Kadmany81cee052012-11-29 14:15:57 +020081 * Max allowed TSPP buffers/descriptors.
82 * If SPS desc FIFO holds X descriptors, we can queue up to X-1 descriptors.
83 */
84#define TSPP_NUM_BUFFERS (TSPP_SPS_DESCRIPTOR_COUNT - 1)
Joel Nider5556a852011-10-16 10:52:13 +020085#define TSPP_TSIF_DEFAULT_TIME_LIMIT 60
86#define SPS_DESCRIPTOR_SIZE 8
87#define MIN_ACCEPTABLE_BUFFER_COUNT 2
Joel Nider435ad8e2011-12-14 16:53:30 +020088#define TSPP_DEBUG(msg...)
Joel Nider5556a852011-10-16 10:52:13 +020089
90/*
91 * TSIF register offsets
92 */
93#define TSIF_STS_CTL_OFF (0x0)
94#define TSIF_TIME_LIMIT_OFF (0x4)
95#define TSIF_CLK_REF_OFF (0x8)
96#define TSIF_LPBK_FLAGS_OFF (0xc)
97#define TSIF_LPBK_DATA_OFF (0x10)
98#define TSIF_TEST_CTL_OFF (0x14)
99#define TSIF_TEST_MODE_OFF (0x18)
100#define TSIF_TEST_RESET_OFF (0x1c)
101#define TSIF_TEST_EXPORT_OFF (0x20)
102#define TSIF_TEST_CURRENT_OFF (0x24)
103
104#define TSIF_DATA_PORT_OFF (0x100)
105
106/* bits for TSIF_STS_CTL register */
107#define TSIF_STS_CTL_EN_IRQ BIT(28)
108#define TSIF_STS_CTL_PACK_AVAIL BIT(27)
109#define TSIF_STS_CTL_1ST_PACKET BIT(26)
110#define TSIF_STS_CTL_OVERFLOW BIT(25)
111#define TSIF_STS_CTL_LOST_SYNC BIT(24)
112#define TSIF_STS_CTL_TIMEOUT BIT(23)
113#define TSIF_STS_CTL_INV_SYNC BIT(21)
114#define TSIF_STS_CTL_INV_NULL BIT(20)
115#define TSIF_STS_CTL_INV_ERROR BIT(19)
116#define TSIF_STS_CTL_INV_ENABLE BIT(18)
117#define TSIF_STS_CTL_INV_DATA BIT(17)
118#define TSIF_STS_CTL_INV_CLOCK BIT(16)
119#define TSIF_STS_CTL_SPARE BIT(15)
120#define TSIF_STS_CTL_EN_NULL BIT(11)
121#define TSIF_STS_CTL_EN_ERROR BIT(10)
122#define TSIF_STS_CTL_LAST_BIT BIT(9)
123#define TSIF_STS_CTL_EN_TIME_LIM BIT(8)
124#define TSIF_STS_CTL_EN_TCR BIT(7)
125#define TSIF_STS_CTL_TEST_MODE BIT(6)
Joel Nider435ad8e2011-12-14 16:53:30 +0200126#define TSIF_STS_CTL_MODE_2 BIT(5)
Joel Nider5556a852011-10-16 10:52:13 +0200127#define TSIF_STS_CTL_EN_DM BIT(4)
128#define TSIF_STS_CTL_STOP BIT(3)
129#define TSIF_STS_CTL_START BIT(0)
130
131/*
132 * TSPP register offsets
133 */
Liron Kuch72b78552012-10-30 17:47:50 +0200134#define TSPP_RST 0x00
Joel Nider5556a852011-10-16 10:52:13 +0200135#define TSPP_CLK_CONTROL 0x04
Liron Kuch72b78552012-10-30 17:47:50 +0200136#define TSPP_CONFIG 0x08
137#define TSPP_CONTROL 0x0C
Joel Nider5556a852011-10-16 10:52:13 +0200138#define TSPP_PS_DISABLE 0x10
Liron Kuch72b78552012-10-30 17:47:50 +0200139#define TSPP_MSG_IRQ_STATUS 0x14
Joel Nider5556a852011-10-16 10:52:13 +0200140#define TSPP_MSG_IRQ_MASK 0x18
141#define TSPP_IRQ_STATUS 0x1C
142#define TSPP_IRQ_MASK 0x20
143#define TSPP_IRQ_CLEAR 0x24
144#define TSPP_PIPE_ERROR_STATUS(_n) (0x28 + (_n << 2))
Liron Kuch72b78552012-10-30 17:47:50 +0200145#define TSPP_STATUS 0x68
146#define TSPP_CURR_TSP_HEADER 0x6C
147#define TSPP_CURR_PID_FILTER 0x70
148#define TSPP_SYSTEM_KEY(_n) (0x74 + (_n << 2))
149#define TSPP_CBC_INIT_VAL(_n) (0x94 + (_n << 2))
150#define TSPP_DATA_KEY_RESET 0x9C
Joel Nider5556a852011-10-16 10:52:13 +0200151#define TSPP_KEY_VALID 0xA0
152#define TSPP_KEY_ERROR 0xA4
153#define TSPP_TEST_CTRL 0xA8
Liron Kuch72b78552012-10-30 17:47:50 +0200154#define TSPP_VERSION 0xAC
Joel Nider5556a852011-10-16 10:52:13 +0200155#define TSPP_GENERICS 0xB0
Liron Kuch72b78552012-10-30 17:47:50 +0200156#define TSPP_NOP 0xB4
Joel Nider5556a852011-10-16 10:52:13 +0200157
158/*
159 * Register bit definitions
160 */
161/* TSPP_RST */
162#define TSPP_RST_RESET BIT(0)
163
164/* TSPP_CLK_CONTROL */
165#define TSPP_CLK_CONTROL_FORCE_CRYPTO BIT(9)
166#define TSPP_CLK_CONTROL_FORCE_PES_PL BIT(8)
167#define TSPP_CLK_CONTROL_FORCE_PES_AF BIT(7)
168#define TSPP_CLK_CONTROL_FORCE_RAW_CTRL BIT(6)
169#define TSPP_CLK_CONTROL_FORCE_PERF_CNT BIT(5)
170#define TSPP_CLK_CONTROL_FORCE_CTX_SEARCH BIT(4)
171#define TSPP_CLK_CONTROL_FORCE_TSP_PROC BIT(3)
172#define TSPP_CLK_CONTROL_FORCE_CONS_AHB2MEM BIT(2)
173#define TSPP_CLK_CONTROL_FORCE_TS_AHB2MEM BIT(1)
174#define TSPP_CLK_CONTROL_SET_CLKON BIT(0)
175
176/* TSPP_CONFIG */
177#define TSPP_CONFIG_SET_PACKET_LENGTH(_a, _b) (_a = (_a & 0xF0) | \
178((_b & 0xF) << 8))
179#define TSPP_CONFIG_GET_PACKET_LENGTH(_a) ((_a >> 8) & 0xF)
180#define TSPP_CONFIG_DUP_WITH_DISC_EN BIT(7)
181#define TSPP_CONFIG_PES_SYNC_ERROR_MASK BIT(6)
182#define TSPP_CONFIG_PS_LEN_ERR_MASK BIT(5)
183#define TSPP_CONFIG_PS_CONT_ERR_UNSP_MASK BIT(4)
184#define TSPP_CONFIG_PS_CONT_ERR_MASK BIT(3)
185#define TSPP_CONFIG_PS_DUP_TSP_MASK BIT(2)
186#define TSPP_CONFIG_TSP_ERR_IND_MASK BIT(1)
187#define TSPP_CONFIG_TSP_SYNC_ERR_MASK BIT(0)
188
189/* TSPP_CONTROL */
190#define TSPP_CONTROL_PID_FILTER_LOCK BIT(5)
191#define TSPP_CONTROL_FORCE_KEY_CALC BIT(4)
192#define TSPP_CONTROL_TSP_CONS_SRC_DIS BIT(3)
193#define TSPP_CONTROL_TSP_TSIF1_SRC_DIS BIT(2)
194#define TSPP_CONTROL_TSP_TSIF0_SRC_DIS BIT(1)
195#define TSPP_CONTROL_PERF_COUNT_INIT BIT(0)
196
197/* TSPP_MSG_IRQ_STATUS + TSPP_MSG_IRQ_MASK */
198#define TSPP_MSG_TSPP_IRQ BIT(2)
199#define TSPP_MSG_TSIF_1_IRQ BIT(1)
200#define TSPP_MSG_TSIF_0_IRQ BIT(0)
201
202/* TSPP_IRQ_STATUS + TSPP_IRQ_MASK + TSPP_IRQ_CLEAR */
Liron Kuch72b78552012-10-30 17:47:50 +0200203#define TSPP_IRQ_STATUS_TSP_RD_CMPL BIT(19)
204#define TSPP_IRQ_STATUS_KEY_ERROR BIT(18)
Joel Nider5556a852011-10-16 10:52:13 +0200205#define TSPP_IRQ_STATUS_KEY_SWITCHED_BAD BIT(17)
206#define TSPP_IRQ_STATUS_KEY_SWITCHED BIT(16)
207#define TSPP_IRQ_STATUS_PS_BROKEN(_n) BIT((_n))
208
209/* TSPP_PIPE_ERROR_STATUS */
Liron Kuch72b78552012-10-30 17:47:50 +0200210#define TSPP_PIPE_PES_SYNC_ERROR BIT(3)
211#define TSPP_PIPE_PS_LENGTH_ERROR BIT(2)
Joel Nider5556a852011-10-16 10:52:13 +0200212#define TSPP_PIPE_PS_CONTINUITY_ERROR BIT(1)
Liron Kuch72b78552012-10-30 17:47:50 +0200213#define TSPP_PIP_PS_LOST_START BIT(0)
Joel Nider5556a852011-10-16 10:52:13 +0200214
215/* TSPP_STATUS */
Liron Kuch72b78552012-10-30 17:47:50 +0200216#define TSPP_STATUS_TSP_PKT_AVAIL BIT(10)
217#define TSPP_STATUS_TSIF1_DM_REQ BIT(6)
218#define TSPP_STATUS_TSIF0_DM_REQ BIT(2)
219#define TSPP_CURR_FILTER_TABLE BIT(0)
Joel Nider5556a852011-10-16 10:52:13 +0200220
221/* TSPP_GENERICS */
Liron Kuch72b78552012-10-30 17:47:50 +0200222#define TSPP_GENERICS_CRYPTO_GEN BIT(12)
Joel Nider5556a852011-10-16 10:52:13 +0200223#define TSPP_GENERICS_MAX_CONS_PIPES BIT(7)
Liron Kuch72b78552012-10-30 17:47:50 +0200224#define TSPP_GENERICS_MAX_PIPES BIT(2)
225#define TSPP_GENERICS_TSIF_1_GEN BIT(1)
226#define TSPP_GENERICS_TSIF_0_GEN BIT(0)
Joel Nider5556a852011-10-16 10:52:13 +0200227
228/*
229 * TSPP memory regions
230 */
231#define TSPP_PID_FILTER_TABLE0 0x800
232#define TSPP_PID_FILTER_TABLE1 0x880
233#define TSPP_PID_FILTER_TABLE2 0x900
234#define TSPP_GLOBAL_PERFORMANCE 0x980 /* see tspp_global_performance */
235#define TSPP_PIPE_CONTEXT 0x990 /* see tspp_pipe_context */
236#define TSPP_PIPE_PERFORMANCE 0x998 /* see tspp_pipe_performance */
237#define TSPP_TSP_BUFF_WORD(_n) (0xC10 + (_n << 2))
238#define TSPP_DATA_KEY 0xCD0
239
Joel Nider5556a852011-10-16 10:52:13 +0200240struct debugfs_entry {
241 const char *name;
242 mode_t mode;
243 int offset;
244};
245
246static const struct debugfs_entry debugfs_tsif_regs[] = {
247 {"sts_ctl", S_IRUGO | S_IWUSR, TSIF_STS_CTL_OFF},
248 {"time_limit", S_IRUGO | S_IWUSR, TSIF_TIME_LIMIT_OFF},
249 {"clk_ref", S_IRUGO | S_IWUSR, TSIF_CLK_REF_OFF},
250 {"lpbk_flags", S_IRUGO | S_IWUSR, TSIF_LPBK_FLAGS_OFF},
251 {"lpbk_data", S_IRUGO | S_IWUSR, TSIF_LPBK_DATA_OFF},
252 {"test_ctl", S_IRUGO | S_IWUSR, TSIF_TEST_CTL_OFF},
253 {"test_mode", S_IRUGO | S_IWUSR, TSIF_TEST_MODE_OFF},
254 {"test_reset", S_IWUSR, TSIF_TEST_RESET_OFF},
255 {"test_export", S_IRUGO | S_IWUSR, TSIF_TEST_EXPORT_OFF},
256 {"test_current", S_IRUGO, TSIF_TEST_CURRENT_OFF},
257 {"data_port", S_IRUSR, TSIF_DATA_PORT_OFF},
258};
259
260static const struct debugfs_entry debugfs_tspp_regs[] = {
261 {"rst", S_IRUGO | S_IWUSR, TSPP_RST},
262 {"clk_control", S_IRUGO | S_IWUSR, TSPP_CLK_CONTROL},
263 {"config", S_IRUGO | S_IWUSR, TSPP_CONFIG},
264 {"control", S_IRUGO | S_IWUSR, TSPP_CONTROL},
265 {"ps_disable", S_IRUGO | S_IWUSR, TSPP_PS_DISABLE},
266 {"msg_irq_status", S_IRUGO | S_IWUSR, TSPP_MSG_IRQ_STATUS},
267 {"msg_irq_mask", S_IRUGO | S_IWUSR, TSPP_MSG_IRQ_MASK},
268 {"irq_status", S_IRUGO | S_IWUSR, TSPP_IRQ_STATUS},
269 {"irq_mask", S_IRUGO | S_IWUSR, TSPP_IRQ_MASK},
270 {"irq_clear", S_IRUGO | S_IWUSR, TSPP_IRQ_CLEAR},
271 /* {"pipe_error_status",S_IRUGO | S_IWUSR, TSPP_PIPE_ERROR_STATUS}, */
272 {"status", S_IRUGO | S_IWUSR, TSPP_STATUS},
273 {"curr_tsp_header", S_IRUGO | S_IWUSR, TSPP_CURR_TSP_HEADER},
274 {"curr_pid_filter", S_IRUGO | S_IWUSR, TSPP_CURR_PID_FILTER},
275 /* {"system_key", S_IRUGO | S_IWUSR, TSPP_SYSTEM_KEY}, */
276 /* {"cbc_init_val", S_IRUGO | S_IWUSR, TSPP_CBC_INIT_VAL}, */
277 {"data_key_reset", S_IRUGO | S_IWUSR, TSPP_DATA_KEY_RESET},
278 {"key_valid", S_IRUGO | S_IWUSR, TSPP_KEY_VALID},
279 {"key_error", S_IRUGO | S_IWUSR, TSPP_KEY_ERROR},
280 {"test_ctrl", S_IRUGO | S_IWUSR, TSPP_TEST_CTRL},
281 {"version", S_IRUGO | S_IWUSR, TSPP_VERSION},
282 {"generics", S_IRUGO | S_IWUSR, TSPP_GENERICS},
283 {"pid_filter_table0", S_IRUGO | S_IWUSR, TSPP_PID_FILTER_TABLE0},
284 {"pid_filter_table1", S_IRUGO | S_IWUSR, TSPP_PID_FILTER_TABLE1},
285 {"pid_filter_table2", S_IRUGO | S_IWUSR, TSPP_PID_FILTER_TABLE2},
286 {"global_performance", S_IRUGO | S_IWUSR, TSPP_GLOBAL_PERFORMANCE},
287 {"pipe_context", S_IRUGO | S_IWUSR, TSPP_PIPE_CONTEXT},
288 {"pipe_performance", S_IRUGO | S_IWUSR, TSPP_PIPE_PERFORMANCE},
289 {"data_key", S_IRUGO | S_IWUSR, TSPP_DATA_KEY}
290};
291
Joel Nider5556a852011-10-16 10:52:13 +0200292struct tspp_pid_filter {
293 u32 filter; /* see FILTER_ macros */
294 u32 config; /* see FILTER_ macros */
295};
296
297/* tsp_info */
298#define FILTER_HEADER_ERROR_MASK BIT(7)
299#define FILTER_TRANS_END_DISABLE BIT(6)
300#define FILTER_DEC_ON_ERROR_EN BIT(5)
301#define FILTER_DECRYPT BIT(4)
302#define FILTER_HAS_ENCRYPTION(_p) (_p->config & FILTER_DECRYPT)
303#define FILTER_GET_PIPE_NUMBER0(_p) (_p->config & 0xF)
304#define FILTER_SET_PIPE_NUMBER0(_p, _b) (_p->config = \
305 (_p->config & ~0xF) | (_b & 0xF))
306#define FILTER_GET_PIPE_PROCESS0(_p) ((_p->filter >> 30) & 0x3)
307#define FILTER_SET_PIPE_PROCESS0(_p, _b) (_p->filter = \
308 (_p->filter & ~(0x3<<30)) | ((_b & 0x3) << 30))
309#define FILTER_GET_PIPE_PID(_p) ((_p->filter >> 13) & 0x1FFF)
310#define FILTER_SET_PIPE_PID(_p, _b) (_p->filter = \
311 (_p->filter & ~(0x1FFF<<13)) | ((_b & 0x1FFF) << 13))
312#define FILTER_GET_PID_MASK(_p) (_p->filter & 0x1FFF)
313#define FILTER_SET_PID_MASK(_p, _b) (_p->filter = \
314 (_p->filter & ~0x1FFF) | (_b & 0x1FFF))
315#define FILTER_GET_PIPE_PROCESS1(_p) ((_p->config >> 30) & 0x3)
316#define FILTER_SET_PIPE_PROCESS1(_p, _b) (_p->config = \
317 (_p->config & ~(0x3<<30)) | ((_b & 0x3) << 30))
318#define FILTER_GET_KEY_NUMBER(_p) ((_p->config >> 8) & 0x7)
319#define FILTER_SET_KEY_NUMBER(_p, _b) (_p->config = \
320 (_p->config & ~(0x7<<8)) | ((_b & 0x7) << 8))
321
322struct tspp_global_performance_regs {
323 u32 tsp_total;
324 u32 tsp_ignored;
325 u32 tsp_error;
326 u32 tsp_sync;
327};
328
329struct tspp_pipe_context_regs {
330 u16 pes_bytes_left;
331 u16 count;
332 u32 tsif_suffix;
333} __packed;
334#define CONTEXT_GET_STATE(_a) (_a & 0x3)
335#define CONTEXT_UNSPEC_LENGTH BIT(11)
336#define CONTEXT_GET_CONT_COUNT(_a) ((_a >> 12) & 0xF)
337
Hamad Kadmany81cee052012-11-29 14:15:57 +0200338#define MSEC_TO_JIFFIES(msec) ((msec) * HZ / 1000)
339
Joel Nider5556a852011-10-16 10:52:13 +0200340struct tspp_pipe_performance_regs {
341 u32 tsp_total;
342 u32 ps_duplicate_tsp;
343 u32 tsp_no_payload;
344 u32 tsp_broken_ps;
345 u32 ps_total_num;
346 u32 ps_continuity_error;
347 u32 ps_length_error;
348 u32 pes_sync_error;
349};
350
351struct tspp_tsif_device {
352 void __iomem *base;
353 u32 time_limit;
354 u32 ref_count;
Joel Nider435ad8e2011-12-14 16:53:30 +0200355 enum tspp_tsif_mode mode;
Hamad Kadmany92705b32012-10-23 14:15:41 +0200356 int clock_inverse;
357 int data_inverse;
358 int sync_inverse;
359 int enable_inverse;
Hamad Kadmany44307d32012-11-25 09:49:51 +0200360 u32 tsif_irq;
Joel Nider5556a852011-10-16 10:52:13 +0200361
362 /* debugfs */
Joel Nider5556a852011-10-16 10:52:13 +0200363 struct dentry *dent_tsif;
364 struct dentry *debugfs_tsif_regs[ARRAY_SIZE(debugfs_tsif_regs)];
Hamad Kadmany44307d32012-11-25 09:49:51 +0200365 u32 stat_rx;
366 u32 stat_overflow;
367 u32 stat_lost_sync;
368 u32 stat_timeout;
Joel Nider5556a852011-10-16 10:52:13 +0200369};
370
371enum tspp_buf_state {
372 TSPP_BUF_STATE_EMPTY, /* buffer has been allocated, but not waiting */
373 TSPP_BUF_STATE_WAITING, /* buffer is waiting to be filled */
Joel Nider435ad8e2011-12-14 16:53:30 +0200374 TSPP_BUF_STATE_DATA, /* buffer is not empty and can be read */
375 TSPP_BUF_STATE_LOCKED /* buffer is being read by a client */
Joel Nider5556a852011-10-16 10:52:13 +0200376};
377
378struct tspp_mem_buffer {
Joel Nider435ad8e2011-12-14 16:53:30 +0200379 struct tspp_mem_buffer *next;
380 struct sps_mem_buffer sps;
381 struct tspp_data_descriptor desc; /* buffer descriptor for kernel api */
Joel Nider5556a852011-10-16 10:52:13 +0200382 enum tspp_buf_state state;
383 size_t filled; /* how much data this buffer is holding */
384 int read_index; /* where to start reading data from */
385};
386
387/* this represents each char device 'channel' */
388struct tspp_channel {
389 struct cdev cdev;
390 struct device *dd;
Joel Nider435ad8e2011-12-14 16:53:30 +0200391 struct tspp_device *pdev; /* can use container_of instead? */
Joel Nider5556a852011-10-16 10:52:13 +0200392 struct sps_pipe *pipe;
393 struct sps_connect config;
394 struct sps_register_event event;
Joel Nider435ad8e2011-12-14 16:53:30 +0200395 struct tspp_mem_buffer *data; /* list of buffers */
396 struct tspp_mem_buffer *read; /* first buffer ready to be read */
397 struct tspp_mem_buffer *waiting; /* first outstanding transfer */
398 struct tspp_mem_buffer *locked; /* buffer currently being read */
Joel Nider5556a852011-10-16 10:52:13 +0200399 wait_queue_head_t in_queue; /* set when data is received */
Joel Nider435ad8e2011-12-14 16:53:30 +0200400 u32 id; /* channel id (0-15) */
401 int used; /* is this channel in use? */
402 int key; /* which encryption key index is used */
403 u32 buffer_size; /* size of the sps transfer buffers */
404 u32 max_buffers; /* how many buffers should be allocated */
405 u32 buffer_count; /* how many buffers are actually allocated */
406 u32 filter_count; /* how many filters have been added to this channel */
407 u32 int_freq; /* generate interrupts every x descriptors */
Joel Nider5556a852011-10-16 10:52:13 +0200408 enum tspp_source src;
409 enum tspp_mode mode;
Joel Nider435ad8e2011-12-14 16:53:30 +0200410 tspp_notifier *notifier; /* used only with kernel api */
411 void *notify_data; /* data to be passed with the notifier */
Hamad Kadmany81cee052012-11-29 14:15:57 +0200412 u32 expiration_period_ms; /* notification on partially filled buffers */
413 struct timer_list expiration_timer;
Hamad Kadmany090709b2013-01-06 12:08:13 +0200414 struct dma_pool *dma_pool;
Liron Kuch72b78552012-10-30 17:47:50 +0200415 tspp_memfree *memfree; /* user defined memory free function */
416 void *user_info; /* user cookie passed to memory alloc/free function */
Joel Nider5556a852011-10-16 10:52:13 +0200417};
418
419struct tspp_pid_filter_table {
420 struct tspp_pid_filter filter[TSPP_NUM_PRIORITIES];
421};
422
423struct tspp_key_entry {
424 u32 even_lsb;
425 u32 even_msb;
426 u32 odd_lsb;
427 u32 odd_msb;
428};
429
430struct tspp_key_table {
431 struct tspp_key_entry entry[TSPP_NUM_KEYS];
432};
433
Joel Nider435ad8e2011-12-14 16:53:30 +0200434/* this represents the actual hardware device */
435struct tspp_device {
436 struct list_head devlist; /* list of all devices */
437 struct platform_device *pdev;
438 void __iomem *base;
439 unsigned int tspp_irq;
440 unsigned int bam_irq;
441 u32 bam_handle;
442 struct sps_bam_props bam_props;
443 struct wake_lock wake_lock;
444 spinlock_t spinlock;
445 struct tasklet_struct tlet;
446 struct tspp_tsif_device tsif[TSPP_TSIF_INSTANCES];
447 /* clocks */
448 struct clk *tsif_pclk;
449 struct clk *tsif_ref_clk;
450 /* data */
451 struct tspp_pid_filter_table *filters[TSPP_FILTER_TABLES];
452 struct tspp_channel channels[TSPP_NUM_CHANNELS];
453 struct tspp_key_table *tspp_key_table;
454 struct tspp_global_performance_regs *tspp_global_performance;
455 struct tspp_pipe_context_regs *tspp_pipe_context;
456 struct tspp_pipe_performance_regs *tspp_pipe_performance;
457
458 struct dentry *dent;
459 struct dentry *debugfs_regs[ARRAY_SIZE(debugfs_tspp_regs)];
460};
461
462
Joel Nider5556a852011-10-16 10:52:13 +0200463static struct class *tspp_class;
464static int tspp_key_entry;
465static dev_t tspp_minor; /* next minor number to assign */
Joel Nider435ad8e2011-12-14 16:53:30 +0200466
467static LIST_HEAD(tspp_devices);
468
469/* forward declarations */
470static ssize_t tspp_read(struct file *, char __user *, size_t, loff_t *);
471static ssize_t tspp_open(struct inode *inode, struct file *filp);
472static unsigned int tspp_poll(struct file *filp, struct poll_table_struct *p);
473static ssize_t tspp_release(struct inode *inode, struct file *filp);
474static long tspp_ioctl(struct file *, unsigned int, unsigned long);
475
476/* file operations */
477static const struct file_operations tspp_fops = {
478 .owner = THIS_MODULE,
479 .read = tspp_read,
480 .open = tspp_open,
481 .poll = tspp_poll,
482 .release = tspp_release,
483 .unlocked_ioctl = tspp_ioctl,
484};
Joel Nider5556a852011-10-16 10:52:13 +0200485
486/*** IRQ ***/
Joel Nider435ad8e2011-12-14 16:53:30 +0200487static irqreturn_t tspp_isr(int irq, void *dev)
Joel Nider5556a852011-10-16 10:52:13 +0200488{
Joel Nider435ad8e2011-12-14 16:53:30 +0200489 struct tspp_device *device = dev;
Joel Nider5556a852011-10-16 10:52:13 +0200490 u32 status, mask;
491 u32 data;
492
493 status = readl_relaxed(device->base + TSPP_IRQ_STATUS);
494 mask = readl_relaxed(device->base + TSPP_IRQ_MASK);
495 status &= mask;
496
497 if (!status) {
498 dev_warn(&device->pdev->dev, "Spurious interrupt");
499 return IRQ_NONE;
500 }
501
502 /* if (status & TSPP_IRQ_STATUS_TSP_RD_CMPL) */
503
504 if (status & TSPP_IRQ_STATUS_KEY_ERROR) {
505 /* read the key error info */
506 data = readl_relaxed(device->base + TSPP_KEY_ERROR);
507 dev_info(&device->pdev->dev, "key error 0x%x", data);
508 }
509 if (status & TSPP_IRQ_STATUS_KEY_SWITCHED_BAD) {
510 data = readl_relaxed(device->base + TSPP_KEY_VALID);
511 dev_info(&device->pdev->dev, "key invalidated: 0x%x", data);
512 }
513 if (status & TSPP_IRQ_STATUS_KEY_SWITCHED)
514 dev_info(&device->pdev->dev, "key switched");
515
516 if (status & 0xffff)
Joel Nider435ad8e2011-12-14 16:53:30 +0200517 dev_info(&device->pdev->dev, "broken pipe %i", status & 0xffff);
Joel Nider5556a852011-10-16 10:52:13 +0200518
519 writel_relaxed(status, device->base + TSPP_IRQ_CLEAR);
Hamad Kadmany44307d32012-11-25 09:49:51 +0200520
521 /*
522 * Before returning IRQ_HANDLED to the generic interrupt handling
523 * framework need to make sure all operations including clearing of
524 * interrupt status registers in the hardware is performed.
525 * Thus a barrier after clearing the interrupt status register
526 * is required to guarantee that the interrupt status register has
527 * really been cleared by the time we return from this handler.
528 */
529 wmb();
530 return IRQ_HANDLED;
531}
532
533static irqreturn_t tsif_isr(int irq, void *dev)
534{
535 struct tspp_tsif_device *tsif_device = dev;
536 u32 sts_ctl = ioread32(tsif_device->base + TSIF_STS_CTL_OFF);
537
538 if (!(sts_ctl & (TSIF_STS_CTL_PACK_AVAIL |
539 TSIF_STS_CTL_OVERFLOW |
540 TSIF_STS_CTL_LOST_SYNC |
541 TSIF_STS_CTL_TIMEOUT)))
542 return IRQ_NONE;
543
544 if (sts_ctl & TSIF_STS_CTL_OVERFLOW)
545 tsif_device->stat_overflow++;
546
547 if (sts_ctl & TSIF_STS_CTL_LOST_SYNC)
548 tsif_device->stat_lost_sync++;
549
550 if (sts_ctl & TSIF_STS_CTL_TIMEOUT)
551 tsif_device->stat_timeout++;
552
553 iowrite32(sts_ctl, tsif_device->base + TSIF_STS_CTL_OFF);
554
555 /*
556 * Before returning IRQ_HANDLED to the generic interrupt handling
557 * framework need to make sure all operations including clearing of
558 * interrupt status registers in the hardware is performed.
559 * Thus a barrier after clearing the interrupt status register
560 * is required to guarantee that the interrupt status register has
561 * really been cleared by the time we return from this handler.
562 */
Joel Nider5556a852011-10-16 10:52:13 +0200563 wmb();
564 return IRQ_HANDLED;
565}
566
567/*** callbacks ***/
568static void tspp_sps_complete_cb(struct sps_event_notify *notify)
569{
Joel Nider435ad8e2011-12-14 16:53:30 +0200570 struct tspp_device *pdev = notify->user;
571 tasklet_schedule(&pdev->tlet);
Joel Nider5556a852011-10-16 10:52:13 +0200572}
573
Hamad Kadmany81cee052012-11-29 14:15:57 +0200574static void tspp_expiration_timer(unsigned long data)
575{
576 struct tspp_device *pdev = (struct tspp_device *)data;
577
578 if (pdev)
579 tasklet_schedule(&pdev->tlet);
580}
581
Joel Nider5556a852011-10-16 10:52:13 +0200582/*** tasklet ***/
583static void tspp_sps_complete_tlet(unsigned long data)
584{
585 int i;
586 int complete;
587 unsigned long flags;
588 struct sps_iovec iovec;
589 struct tspp_channel *channel;
590 struct tspp_device *device = (struct tspp_device *)data;
Joel Nider5556a852011-10-16 10:52:13 +0200591 spin_lock_irqsave(&device->spinlock, flags);
592
593 for (i = 0; i < TSPP_NUM_CHANNELS; i++) {
594 complete = 0;
Joel Nider435ad8e2011-12-14 16:53:30 +0200595 channel = &device->channels[i];
Hamad Kadmany81cee052012-11-29 14:15:57 +0200596
Joel Nider435ad8e2011-12-14 16:53:30 +0200597 if (!channel->used || !channel->waiting)
598 continue;
Joel Nider5556a852011-10-16 10:52:13 +0200599
Hamad Kadmany81cee052012-11-29 14:15:57 +0200600 /* stop the expiration timer */
601 if (channel->expiration_period_ms)
602 del_timer(&channel->expiration_timer);
603
Joel Nider5556a852011-10-16 10:52:13 +0200604 /* get completions */
Joel Nider435ad8e2011-12-14 16:53:30 +0200605 while (channel->waiting->state == TSPP_BUF_STATE_WAITING) {
Joel Nider5556a852011-10-16 10:52:13 +0200606 if (sps_get_iovec(channel->pipe, &iovec) != 0) {
607 pr_err("tspp: Error in iovec on channel %i",
608 channel->id);
609 break;
610 }
611 if (iovec.size == 0)
612 break;
613
Joel Nider435ad8e2011-12-14 16:53:30 +0200614 if (iovec.addr != channel->waiting->sps.phys_base)
Joel Nider5556a852011-10-16 10:52:13 +0200615 pr_err("tspp: buffer mismatch 0x%08x",
Joel Nider435ad8e2011-12-14 16:53:30 +0200616 channel->waiting->sps.phys_base);
Joel Nider5556a852011-10-16 10:52:13 +0200617
618 complete = 1;
Joel Nider435ad8e2011-12-14 16:53:30 +0200619 channel->waiting->state = TSPP_BUF_STATE_DATA;
620 channel->waiting->filled = iovec.size;
621 channel->waiting->read_index = 0;
622
Hamad Kadmany44307d32012-11-25 09:49:51 +0200623 if (channel->src == TSPP_SOURCE_TSIF0)
624 device->tsif[0].stat_rx++;
625 else if (channel->src == TSPP_SOURCE_TSIF1)
626 device->tsif[1].stat_rx++;
627
Joel Nider435ad8e2011-12-14 16:53:30 +0200628 /* update the pointers */
629 channel->waiting = channel->waiting->next;
Joel Nider5556a852011-10-16 10:52:13 +0200630 }
631
Joel Nider435ad8e2011-12-14 16:53:30 +0200632 /* wake any waiting processes */
Joel Nider5556a852011-10-16 10:52:13 +0200633 if (complete) {
Joel Nider5556a852011-10-16 10:52:13 +0200634 wake_up_interruptible(&channel->in_queue);
Joel Nider435ad8e2011-12-14 16:53:30 +0200635
636 /* call notifiers */
637 if (channel->notifier)
638 channel->notifier(channel->id,
639 channel->notify_data);
Joel Nider5556a852011-10-16 10:52:13 +0200640 }
Hamad Kadmany81cee052012-11-29 14:15:57 +0200641
642 /* restart expiration timer */
643 if (channel->expiration_period_ms)
644 mod_timer(&channel->expiration_timer,
645 jiffies +
646 MSEC_TO_JIFFIES(
647 channel->expiration_period_ms));
Joel Nider5556a852011-10-16 10:52:13 +0200648 }
649
650 spin_unlock_irqrestore(&device->spinlock, flags);
651}
652
653/*** GPIO functions ***/
Liron Kuch275c0b32013-02-10 15:19:32 +0200654static int tspp_gpios_disable(const struct tspp_tsif_device *tsif_device,
655 const struct msm_gpio *table,
656 int size)
Joel Nider5556a852011-10-16 10:52:13 +0200657{
658 int rc = 0;
659 int i;
660 const struct msm_gpio *g;
Liron Kuch59339922013-01-01 18:29:47 +0200661
Joel Nider5556a852011-10-16 10:52:13 +0200662 for (i = size-1; i >= 0; i--) {
663 int tmp;
664 g = table + i;
Liron Kuch59339922013-01-01 18:29:47 +0200665
Liron Kuch275c0b32013-02-10 15:19:32 +0200666 /* don't use sync GPIO when not working in mode 2 */
667 if ((tsif_device->mode != TSPP_TSIF_MODE_2) &&
668 (strnstr(g->label, "sync", strlen(g->label)) != NULL))
669 continue;
670
Liron Kuch59339922013-01-01 18:29:47 +0200671 tmp = gpio_tlmm_config(GPIO_CFG(GPIO_PIN(g->gpio_cfg),
672 0, GPIO_CFG_INPUT, GPIO_CFG_PULL_DOWN, GPIO_CFG_2MA),
673 GPIO_CFG_DISABLE);
Joel Nider5556a852011-10-16 10:52:13 +0200674 if (tmp) {
Liron Kuch72b78552012-10-30 17:47:50 +0200675 pr_err("tspp_gpios_disable(0x%08x, GPIO_CFG_DISABLE) <%s> failed: %d\n",
Joel Nider5556a852011-10-16 10:52:13 +0200676 g->gpio_cfg, g->label ?: "?", rc);
677 pr_err("tspp: pin %d func %d dir %d pull %d drvstr %d\n",
678 GPIO_PIN(g->gpio_cfg), GPIO_FUNC(g->gpio_cfg),
679 GPIO_DIR(g->gpio_cfg), GPIO_PULL(g->gpio_cfg),
680 GPIO_DRVSTR(g->gpio_cfg));
681 if (!rc)
682 rc = tmp;
683 }
684 }
685
686 return rc;
687}
688
Liron Kuch275c0b32013-02-10 15:19:32 +0200689static int tspp_gpios_enable(const struct tspp_tsif_device *tsif_device,
690 const struct msm_gpio *table,
691 int size)
Joel Nider5556a852011-10-16 10:52:13 +0200692{
693 int rc;
Liron Kuch275c0b32013-02-10 15:19:32 +0200694 int i;
Joel Nider5556a852011-10-16 10:52:13 +0200695 const struct msm_gpio *g;
Liron Kuch59339922013-01-01 18:29:47 +0200696
Joel Nider5556a852011-10-16 10:52:13 +0200697 for (i = 0; i < size; i++) {
698 g = table + i;
Liron Kuch275c0b32013-02-10 15:19:32 +0200699
700 /* don't use sync GPIO when not working in mode 2 */
701 if ((tsif_device->mode != TSPP_TSIF_MODE_2) &&
702 (strnstr(g->label, "sync", strlen(g->label)) != NULL))
703 continue;
704
Joel Nider5556a852011-10-16 10:52:13 +0200705 rc = gpio_tlmm_config(g->gpio_cfg, GPIO_CFG_ENABLE);
706 if (rc) {
Liron Kuch72b78552012-10-30 17:47:50 +0200707 pr_err("tspp: gpio_tlmm_config(0x%08x, GPIO_CFG_ENABLE) <%s> failed: %d\n",
Joel Nider5556a852011-10-16 10:52:13 +0200708 g->gpio_cfg, g->label ?: "?", rc);
709 pr_err("tspp: pin %d func %d dir %d pull %d drvstr %d\n",
710 GPIO_PIN(g->gpio_cfg), GPIO_FUNC(g->gpio_cfg),
711 GPIO_DIR(g->gpio_cfg), GPIO_PULL(g->gpio_cfg),
712 GPIO_DRVSTR(g->gpio_cfg));
713 goto err;
714 }
715 }
716 return 0;
717err:
Liron Kuch275c0b32013-02-10 15:19:32 +0200718 tspp_gpios_disable(tsif_device, table, i);
Joel Nider5556a852011-10-16 10:52:13 +0200719
Joel Nider5556a852011-10-16 10:52:13 +0200720 return rc;
721}
722
Liron Kuch275c0b32013-02-10 15:19:32 +0200723
724static int tspp_config_gpios(struct tspp_device *device,
725 enum tspp_source source,
726 int enable)
Joel Nider5556a852011-10-16 10:52:13 +0200727{
Liron Kuch275c0b32013-02-10 15:19:32 +0200728 const struct msm_gpio *table;
729 struct msm_tspp_platform_data *pdata = device->pdev->dev.platform_data;
730 int num_gpios = (pdata->num_gpios / TSPP_TSIF_INSTANCES);
731 int i = 0;
Liron Kuch59339922013-01-01 18:29:47 +0200732
Liron Kuch275c0b32013-02-10 15:19:32 +0200733 if (num_gpios != TSPP_GPIOS_PER_TSIF) {
734 pr_err("tspp %s: unexpected number of GPIOs %d, expected %d\n",
735 __func__, num_gpios, TSPP_GPIOS_PER_TSIF);
736 return -EINVAL;
737 }
Joel Nider5556a852011-10-16 10:52:13 +0200738
Liron Kuch275c0b32013-02-10 15:19:32 +0200739 /*
740 * Note: this code assumes that the GPIO definitions in the
741 * pdata->gpios table are according to the TSIF instance number,
742 * i.e., that TSIF0 GPIOs are defined first, then TSIF1 GPIOs etc.
743 */
744 switch (source) {
745 case TSPP_SOURCE_TSIF0:
746 i = 0;
747 break;
748 case TSPP_SOURCE_TSIF1:
749 i = 1;
750 break;
751 default:
752 pr_err("tspp %s: invalid source\n", __func__);
753 return -EINVAL;
754 }
Liron Kuch59339922013-01-01 18:29:47 +0200755
Liron Kuch275c0b32013-02-10 15:19:32 +0200756 table = pdata->gpios + (i * num_gpios);
757 if (enable)
758 return tspp_gpios_enable(&device->tsif[i], table, num_gpios);
759 else
760 return tspp_gpios_disable(&device->tsif[i], table, num_gpios);
Joel Nider5556a852011-10-16 10:52:13 +0200761}
762
Joel Nider435ad8e2011-12-14 16:53:30 +0200763/*** Clock functions ***/
764static int tspp_clock_start(struct tspp_device *device)
765{
Liron Kuch605cc122013-02-21 14:25:57 +0200766 if (device == NULL) {
767 pr_err("tspp: Can't start clocks, invalid device\n");
768 return -EINVAL;
769 }
770
Joel Nider435ad8e2011-12-14 16:53:30 +0200771 if (device->tsif_pclk && clk_prepare_enable(device->tsif_pclk) != 0) {
772 pr_err("tspp: Can't start pclk");
773 return -EBUSY;
774 }
775
776 if (device->tsif_ref_clk &&
777 clk_prepare_enable(device->tsif_ref_clk) != 0) {
778 pr_err("tspp: Can't start ref clk");
779 clk_disable_unprepare(device->tsif_pclk);
780 return -EBUSY;
781 }
782
783 return 0;
784}
785
786static void tspp_clock_stop(struct tspp_device *device)
787{
Liron Kuch605cc122013-02-21 14:25:57 +0200788 if (device == NULL) {
789 pr_err("tspp: Can't stop clocks, invalid device\n");
790 return;
791 }
792
Joel Nider435ad8e2011-12-14 16:53:30 +0200793 if (device->tsif_pclk)
Liron Kuch605cc122013-02-21 14:25:57 +0200794 clk_disable_unprepare(device->tsif_pclk);
Joel Nider435ad8e2011-12-14 16:53:30 +0200795
796 if (device->tsif_ref_clk)
Liron Kuch605cc122013-02-21 14:25:57 +0200797 clk_disable_unprepare(device->tsif_ref_clk);
Joel Nider435ad8e2011-12-14 16:53:30 +0200798}
799
Joel Nider5556a852011-10-16 10:52:13 +0200800/*** TSIF functions ***/
801static int tspp_start_tsif(struct tspp_tsif_device *tsif_device)
802{
803 int start_hardware = 0;
804 u32 ctl;
805
806 if (tsif_device->ref_count == 0) {
807 start_hardware = 1;
808 } else if (tsif_device->ref_count > 0) {
809 ctl = readl_relaxed(tsif_device->base + TSIF_STS_CTL_OFF);
810 if ((ctl & TSIF_STS_CTL_START) != 1) {
811 /* this hardware should already be running */
812 pr_warn("tspp: tsif hw not started but ref count > 0");
813 start_hardware = 1;
814 }
815 }
816
817 if (start_hardware) {
Joel Nider435ad8e2011-12-14 16:53:30 +0200818 ctl = TSIF_STS_CTL_EN_IRQ |
Joel Nider5556a852011-10-16 10:52:13 +0200819 TSIF_STS_CTL_EN_DM;
Hamad Kadmany92705b32012-10-23 14:15:41 +0200820
821 if (tsif_device->clock_inverse)
822 ctl |= TSIF_STS_CTL_INV_CLOCK;
823
824 if (tsif_device->data_inverse)
825 ctl |= TSIF_STS_CTL_INV_DATA;
826
827 if (tsif_device->sync_inverse)
828 ctl |= TSIF_STS_CTL_INV_SYNC;
829
830 if (tsif_device->enable_inverse)
831 ctl |= TSIF_STS_CTL_INV_ENABLE;
832
Joel Nider435ad8e2011-12-14 16:53:30 +0200833 switch (tsif_device->mode) {
834 case TSPP_TSIF_MODE_LOOPBACK:
835 ctl |= TSIF_STS_CTL_EN_NULL |
836 TSIF_STS_CTL_EN_ERROR |
837 TSIF_STS_CTL_TEST_MODE;
838 break;
839 case TSPP_TSIF_MODE_1:
840 ctl |= TSIF_STS_CTL_EN_TIME_LIM |
841 TSIF_STS_CTL_EN_TCR;
842 break;
843 case TSPP_TSIF_MODE_2:
844 ctl |= TSIF_STS_CTL_EN_TIME_LIM |
845 TSIF_STS_CTL_EN_TCR |
846 TSIF_STS_CTL_MODE_2;
847 break;
848 default:
849 pr_warn("tspp: unknown tsif mode 0x%x",
850 tsif_device->mode);
Joel Nider5556a852011-10-16 10:52:13 +0200851 }
852 writel_relaxed(ctl, tsif_device->base + TSIF_STS_CTL_OFF);
853 writel_relaxed(tsif_device->time_limit,
854 tsif_device->base + TSIF_TIME_LIMIT_OFF);
855 wmb();
856 writel_relaxed(ctl | TSIF_STS_CTL_START,
857 tsif_device->base + TSIF_STS_CTL_OFF);
858 wmb();
Joel Nider5556a852011-10-16 10:52:13 +0200859 }
860
Joel Nider435ad8e2011-12-14 16:53:30 +0200861 ctl = readl_relaxed(tsif_device->base + TSIF_STS_CTL_OFF);
Joel Nider5556a852011-10-16 10:52:13 +0200862 tsif_device->ref_count++;
863
Joel Nider435ad8e2011-12-14 16:53:30 +0200864 return (ctl & TSIF_STS_CTL_START) ? 0 : -EBUSY;
Joel Nider5556a852011-10-16 10:52:13 +0200865}
866
867static void tspp_stop_tsif(struct tspp_tsif_device *tsif_device)
868{
869 if (tsif_device->ref_count == 0)
870 return;
871
872 tsif_device->ref_count--;
873
874 if (tsif_device->ref_count == 0) {
875 writel_relaxed(TSIF_STS_CTL_STOP,
876 tsif_device->base + TSIF_STS_CTL_OFF);
877 wmb();
878 }
879}
880
Joel Nider435ad8e2011-12-14 16:53:30 +0200881/*** local TSPP functions ***/
882static int tspp_channels_in_use(struct tspp_device *pdev)
883{
884 int i;
885 int count = 0;
886 for (i = 0; i < TSPP_NUM_CHANNELS; i++)
887 count += (pdev->channels[i].used ? 1 : 0);
888
889 return count;
890}
891
892static struct tspp_device *tspp_find_by_id(int id)
893{
894 struct tspp_device *dev;
895 list_for_each_entry(dev, &tspp_devices, devlist) {
896 if (dev->pdev->id == id)
897 return dev;
898 }
899 return NULL;
900}
901
Joel Nider5556a852011-10-16 10:52:13 +0200902static int tspp_get_key_entry(void)
903{
904 int i;
905 for (i = 0; i < TSPP_NUM_KEYS; i++) {
906 if (!(tspp_key_entry & (1 << i))) {
907 tspp_key_entry |= (1 << i);
908 return i;
909 }
910 }
Joel Nider435ad8e2011-12-14 16:53:30 +0200911 return 1 < TSPP_NUM_KEYS;
Joel Nider5556a852011-10-16 10:52:13 +0200912}
913
914static void tspp_free_key_entry(int entry)
915{
916 if (entry > TSPP_NUM_KEYS) {
917 pr_err("tspp_free_key_entry: index out of bounds");
918 return;
919 }
920
921 tspp_key_entry &= ~(1 << entry);
922}
923
Joel Nider435ad8e2011-12-14 16:53:30 +0200924static int tspp_alloc_buffer(u32 channel_id, struct tspp_data_descriptor *desc,
Hamad Kadmany090709b2013-01-06 12:08:13 +0200925 u32 size, struct dma_pool *dma_pool, tspp_allocator *alloc, void *user)
Joel Nider5556a852011-10-16 10:52:13 +0200926{
Joel Nider435ad8e2011-12-14 16:53:30 +0200927 if (size < TSPP_MIN_BUFFER_SIZE ||
928 size > TSPP_MAX_BUFFER_SIZE) {
929 pr_err("tspp: bad buffer size %i", size);
Joel Nider5556a852011-10-16 10:52:13 +0200930 return -ENOMEM;
931 }
Joel Nider435ad8e2011-12-14 16:53:30 +0200932
933 if (alloc) {
934 TSPP_DEBUG("tspp using alloc function");
935 desc->virt_base = alloc(channel_id, size,
936 &desc->phys_base, user);
937 } else {
Hamad Kadmany090709b2013-01-06 12:08:13 +0200938 if (!dma_pool)
939 desc->virt_base = dma_alloc_coherent(NULL, size,
940 &desc->phys_base, GFP_KERNEL);
941 else
942 desc->virt_base = dma_pool_alloc(dma_pool, GFP_KERNEL,
943 &desc->phys_base);
944
Liron Kuch72b78552012-10-30 17:47:50 +0200945 if (desc->virt_base == 0) {
Hamad Kadmany090709b2013-01-06 12:08:13 +0200946 pr_err("tspp: dma buffer allocation failed %i\n", size);
Liron Kuch72b78552012-10-30 17:47:50 +0200947 return -ENOMEM;
948 }
Joel Nider435ad8e2011-12-14 16:53:30 +0200949 }
950
951 desc->size = size;
952 return 0;
953}
954
955static int tspp_queue_buffer(struct tspp_channel *channel,
956 struct tspp_mem_buffer *buffer)
957{
958 int rc;
959 u32 flags = 0;
960
961 /* make sure the interrupt frequency is valid */
962 if (channel->int_freq < 1)
963 channel->int_freq = 1;
964
965 /* generate interrupt according to requested frequency */
966 if (buffer->desc.id % channel->int_freq == channel->int_freq-1)
Hamad Kadmany81cee052012-11-29 14:15:57 +0200967 flags = SPS_IOVEC_FLAG_INT;
Joel Nider435ad8e2011-12-14 16:53:30 +0200968
969 /* start the transfer */
970 rc = sps_transfer_one(channel->pipe,
971 buffer->sps.phys_base,
972 buffer->sps.size,
973 channel->pdev,
974 flags);
975 if (rc < 0)
976 return rc;
977
978 buffer->state = TSPP_BUF_STATE_WAITING;
Joel Nider5556a852011-10-16 10:52:13 +0200979
980 return 0;
981}
982
983static int tspp_global_reset(struct tspp_device *pdev)
984{
985 u32 i, val;
986
987 /* stop all TSIFs */
988 for (i = 0; i < TSPP_TSIF_INSTANCES; i++) {
989 pdev->tsif[i].ref_count = 1; /* allows stopping hw */
990 tspp_stop_tsif(&pdev->tsif[i]); /* will reset ref_count to 0 */
991 pdev->tsif[i].time_limit = TSPP_TSIF_DEFAULT_TIME_LIMIT;
Hamad Kadmany92705b32012-10-23 14:15:41 +0200992 pdev->tsif[i].clock_inverse = 0;
993 pdev->tsif[i].data_inverse = 0;
994 pdev->tsif[i].sync_inverse = 0;
995 pdev->tsif[i].enable_inverse = 0;
Joel Nider5556a852011-10-16 10:52:13 +0200996 }
997 writel_relaxed(TSPP_RST_RESET, pdev->base + TSPP_RST);
998 wmb();
999
1000 /* BAM */
1001 if (sps_device_reset(pdev->bam_handle) != 0) {
1002 pr_err("tspp: error resetting bam");
Joel Nider435ad8e2011-12-14 16:53:30 +02001003 return -EBUSY;
Joel Nider5556a852011-10-16 10:52:13 +02001004 }
1005
1006 /* TSPP tables */
1007 for (i = 0; i < TSPP_FILTER_TABLES; i++)
Joel Nider435ad8e2011-12-14 16:53:30 +02001008 memset(pdev->filters[i],
Joel Nider5556a852011-10-16 10:52:13 +02001009 0, sizeof(struct tspp_pid_filter_table));
1010
1011 /* disable all filters */
1012 val = (2 << TSPP_NUM_CHANNELS) - 1;
1013 writel_relaxed(val, pdev->base + TSPP_PS_DISABLE);
1014
1015 /* TSPP registers */
1016 val = readl_relaxed(pdev->base + TSPP_CONTROL);
1017 writel_relaxed(val | TSPP_CLK_CONTROL_FORCE_PERF_CNT,
1018 pdev->base + TSPP_CONTROL);
1019 wmb();
Joel Nider435ad8e2011-12-14 16:53:30 +02001020 memset(pdev->tspp_global_performance, 0,
Joel Nider5556a852011-10-16 10:52:13 +02001021 sizeof(struct tspp_global_performance_regs));
Joel Nider435ad8e2011-12-14 16:53:30 +02001022 memset(pdev->tspp_pipe_context, 0,
Joel Nider5556a852011-10-16 10:52:13 +02001023 sizeof(struct tspp_pipe_context_regs));
Joel Nider435ad8e2011-12-14 16:53:30 +02001024 memset(pdev->tspp_pipe_performance, 0,
Joel Nider5556a852011-10-16 10:52:13 +02001025 sizeof(struct tspp_pipe_performance_regs));
1026 wmb();
1027 writel_relaxed(val & ~TSPP_CLK_CONTROL_FORCE_PERF_CNT,
1028 pdev->base + TSPP_CONTROL);
1029 wmb();
1030
1031 val = readl_relaxed(pdev->base + TSPP_CONFIG);
1032 val &= ~(TSPP_CONFIG_PS_LEN_ERR_MASK |
1033 TSPP_CONFIG_PS_CONT_ERR_UNSP_MASK |
1034 TSPP_CONFIG_PS_CONT_ERR_MASK);
1035 TSPP_CONFIG_SET_PACKET_LENGTH(val, TSPP_PACKET_LENGTH);
1036 writel_relaxed(val, pdev->base + TSPP_CONFIG);
Hamad Kadmany6bac7832012-12-20 18:30:40 +02001037 writel_relaxed(0x0007ffff, pdev->base + TSPP_IRQ_MASK);
Joel Nider5556a852011-10-16 10:52:13 +02001038 writel_relaxed(0x000fffff, pdev->base + TSPP_IRQ_CLEAR);
1039 writel_relaxed(0, pdev->base + TSPP_RST);
1040 wmb();
1041
1042 tspp_key_entry = 0;
1043
1044 return 0;
1045}
1046
Joel Nider435ad8e2011-12-14 16:53:30 +02001047static int tspp_select_source(u32 dev, u32 channel_id,
1048 struct tspp_select_source *src)
1049{
1050 /* make sure the requested src id is in bounds */
1051 if (src->source > TSPP_SOURCE_MEM) {
1052 pr_err("tspp source out of bounds");
1053 return -EINVAL;
1054 }
1055
1056 /* open the stream */
Hamad Kadmany92705b32012-10-23 14:15:41 +02001057 tspp_open_stream(dev, channel_id, src);
Joel Nider435ad8e2011-12-14 16:53:30 +02001058
1059 return 0;
1060}
1061
1062static int tspp_set_iv(struct tspp_channel *channel, struct tspp_iv *iv)
1063{
1064 struct tspp_device *pdev = channel->pdev;
1065
1066 writel_relaxed(iv->data[0], pdev->base + TSPP_CBC_INIT_VAL(0));
1067 writel_relaxed(iv->data[1], pdev->base + TSPP_CBC_INIT_VAL(1));
1068 return 0;
1069}
1070
1071static int tspp_set_system_keys(struct tspp_channel *channel,
1072 struct tspp_system_keys *keys)
1073{
1074 int i;
1075 struct tspp_device *pdev = channel->pdev;
1076
1077 for (i = 0; i < TSPP_NUM_SYSTEM_KEYS; i++)
1078 writel_relaxed(keys->data[i], pdev->base + TSPP_SYSTEM_KEY(i));
1079
1080 return 0;
1081}
1082
1083static int tspp_channel_init(struct tspp_channel *channel,
1084 struct tspp_device *pdev)
1085{
1086 channel->cdev.owner = THIS_MODULE;
1087 cdev_init(&channel->cdev, &tspp_fops);
1088 channel->pdev = pdev;
1089 channel->data = NULL;
1090 channel->read = NULL;
1091 channel->waiting = NULL;
1092 channel->locked = NULL;
1093 channel->id = MINOR(tspp_minor);
1094 channel->used = 0;
1095 channel->buffer_size = TSPP_MIN_BUFFER_SIZE;
1096 channel->max_buffers = TSPP_NUM_BUFFERS;
1097 channel->buffer_count = 0;
1098 channel->filter_count = 0;
1099 channel->int_freq = 1;
Liron Kuch72b78552012-10-30 17:47:50 +02001100 channel->src = TSPP_SOURCE_NONE;
1101 channel->mode = TSPP_MODE_DISABLED;
Joel Nider435ad8e2011-12-14 16:53:30 +02001102 channel->notifier = NULL;
1103 channel->notify_data = NULL;
Hamad Kadmany81cee052012-11-29 14:15:57 +02001104 channel->expiration_period_ms = 0;
Liron Kuch72b78552012-10-30 17:47:50 +02001105 channel->memfree = NULL;
1106 channel->user_info = NULL;
Joel Nider435ad8e2011-12-14 16:53:30 +02001107 init_waitqueue_head(&channel->in_queue);
1108
1109 if (cdev_add(&channel->cdev, tspp_minor++, 1) != 0) {
1110 pr_err("tspp: cdev_add failed");
1111 return -EBUSY;
1112 }
1113
1114 channel->dd = device_create(tspp_class, NULL, channel->cdev.dev,
1115 channel, "tspp%02d", channel->id);
1116 if (IS_ERR(channel->dd)) {
1117 pr_err("tspp: device_create failed: %i",
1118 (int)PTR_ERR(channel->dd));
1119 cdev_del(&channel->cdev);
1120 return -EBUSY;
1121 }
1122
1123 return 0;
1124}
1125
1126static int tspp_set_buffer_size(struct tspp_channel *channel,
1127 struct tspp_buffer *buf)
1128{
Liron Kuch72b78552012-10-30 17:47:50 +02001129 if (channel->buffer_count > 0) {
1130 pr_err("tspp: cannot set buffer size - buffers already allocated\n");
1131 return -EPERM;
1132 }
1133
Joel Nider435ad8e2011-12-14 16:53:30 +02001134 if (buf->size < TSPP_MIN_BUFFER_SIZE)
1135 channel->buffer_size = TSPP_MIN_BUFFER_SIZE;
1136 else if (buf->size > TSPP_MAX_BUFFER_SIZE)
1137 channel->buffer_size = TSPP_MAX_BUFFER_SIZE;
1138 else
1139 channel->buffer_size = buf->size;
1140
1141 return 0;
1142}
1143
1144static void tspp_set_tsif_mode(struct tspp_channel *channel,
1145 enum tspp_tsif_mode mode)
1146{
1147 int index;
1148
1149 switch (channel->src) {
1150 case TSPP_SOURCE_TSIF0:
1151 index = 0;
1152 break;
1153 case TSPP_SOURCE_TSIF1:
1154 index = 1;
1155 break;
1156 default:
1157 pr_warn("tspp: can't set mode for non-tsif source %d",
1158 channel->src);
1159 return;
1160 }
1161 channel->pdev->tsif[index].mode = mode;
1162}
1163
Hamad Kadmany92705b32012-10-23 14:15:41 +02001164static void tspp_set_signal_inversion(struct tspp_channel *channel,
Liron Kuch72b78552012-10-30 17:47:50 +02001165 int clock_inverse, int data_inverse,
1166 int sync_inverse, int enable_inverse)
Hamad Kadmany92705b32012-10-23 14:15:41 +02001167{
1168 int index;
1169
1170 switch (channel->src) {
1171 case TSPP_SOURCE_TSIF0:
1172 index = 0;
1173 break;
1174 case TSPP_SOURCE_TSIF1:
1175 index = 1;
1176 break;
1177 default:
1178 return;
1179 }
1180 channel->pdev->tsif[index].clock_inverse = clock_inverse;
1181 channel->pdev->tsif[index].data_inverse = data_inverse;
1182 channel->pdev->tsif[index].sync_inverse = sync_inverse;
1183 channel->pdev->tsif[index].enable_inverse = enable_inverse;
1184}
1185
Liron Kuch72b78552012-10-30 17:47:50 +02001186static int tspp_is_buffer_size_aligned(u32 size, enum tspp_mode mode)
1187{
1188 u32 alignment;
1189
1190 switch (mode) {
1191 case TSPP_MODE_RAW:
1192 /* must be a multiple of 192 */
1193 alignment = (TSPP_PACKET_LENGTH + 4);
1194 if (size % alignment)
1195 return 0;
1196 return 1;
1197
1198 case TSPP_MODE_RAW_NO_SUFFIX:
1199 /* must be a multiple of 188 */
1200 alignment = TSPP_PACKET_LENGTH;
1201 if (size % alignment)
1202 return 0;
1203 return 1;
1204
1205 case TSPP_MODE_DISABLED:
1206 case TSPP_MODE_PES:
1207 default:
1208 /* no alignment requirement */
1209 return 1;
1210 }
1211
1212}
1213
1214static u32 tspp_align_buffer_size_by_mode(u32 size, enum tspp_mode mode)
1215{
1216 u32 new_size;
1217 u32 alignment;
1218
1219 switch (mode) {
1220 case TSPP_MODE_RAW:
1221 /* must be a multiple of 192 */
1222 alignment = (TSPP_PACKET_LENGTH + 4);
1223 break;
1224
1225 case TSPP_MODE_RAW_NO_SUFFIX:
1226 /* must be a multiple of 188 */
1227 alignment = TSPP_PACKET_LENGTH;
1228 break;
1229
1230 case TSPP_MODE_DISABLED:
1231 case TSPP_MODE_PES:
1232 default:
1233 /* no alignment requirement - give the user what he asks for */
1234 alignment = 1;
1235 break;
1236 }
1237 /* align up */
1238 new_size = (((size + alignment - 1) / alignment) * alignment);
1239 return new_size;
1240}
1241
1242static void tspp_destroy_buffers(u32 channel_id, struct tspp_channel *channel)
1243{
1244 int i;
1245 struct tspp_mem_buffer *pbuf, *temp;
1246
1247 pbuf = channel->data;
1248 for (i = 0; i < channel->buffer_count; i++) {
1249 if (pbuf->desc.phys_base) {
1250 if (channel->memfree) {
1251 channel->memfree(channel_id,
1252 pbuf->desc.size,
1253 pbuf->desc.virt_base,
1254 pbuf->desc.phys_base,
1255 channel->user_info);
1256 } else {
Hamad Kadmany090709b2013-01-06 12:08:13 +02001257 if (!channel->dma_pool)
1258 dma_free_coherent(NULL,
1259 pbuf->desc.size,
1260 pbuf->desc.virt_base,
1261 pbuf->desc.phys_base);
1262 else
1263 dma_pool_free(channel->dma_pool,
1264 pbuf->desc.virt_base,
1265 pbuf->desc.phys_base);
Liron Kuch72b78552012-10-30 17:47:50 +02001266 }
1267 pbuf->desc.phys_base = 0;
1268 }
1269 pbuf->desc.virt_base = 0;
1270 pbuf->state = TSPP_BUF_STATE_EMPTY;
1271 temp = pbuf;
1272 pbuf = pbuf->next;
1273 kfree(temp);
1274 }
1275}
1276
Joel Nider435ad8e2011-12-14 16:53:30 +02001277/*** TSPP API functions ***/
Liron Kuch72b78552012-10-30 17:47:50 +02001278
1279/**
1280 * tspp_open_stream - open a TSPP stream for use.
1281 *
1282 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1283 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
1284 * @source: stream source parameters.
1285 *
1286 * Return error status
1287 *
1288 */
1289int tspp_open_stream(u32 dev, u32 channel_id,
1290 struct tspp_select_source *source)
Joel Nider5556a852011-10-16 10:52:13 +02001291{
1292 u32 val;
1293 struct tspp_device *pdev;
Joel Nider435ad8e2011-12-14 16:53:30 +02001294 struct tspp_channel *channel;
Joel Nider5556a852011-10-16 10:52:13 +02001295
Hamad Kadmany92705b32012-10-23 14:15:41 +02001296 TSPP_DEBUG("tspp_open_stream %i %i %i %i",
1297 dev, channel_id, source->source, source->mode);
Liron Kuch72b78552012-10-30 17:47:50 +02001298
Joel Nider435ad8e2011-12-14 16:53:30 +02001299 if (dev >= TSPP_MAX_DEVICES) {
1300 pr_err("tspp: device id out of range");
1301 return -ENODEV;
1302 }
Joel Nider5556a852011-10-16 10:52:13 +02001303
Joel Nider435ad8e2011-12-14 16:53:30 +02001304 if (channel_id >= TSPP_NUM_CHANNELS) {
1305 pr_err("tspp: channel id out of range");
1306 return -ECHRNG;
1307 }
1308
1309 pdev = tspp_find_by_id(dev);
1310 if (!pdev) {
1311 pr_err("tspp_str: can't find device %i", dev);
1312 return -ENODEV;
1313 }
1314 channel = &pdev->channels[channel_id];
Hamad Kadmany92705b32012-10-23 14:15:41 +02001315 channel->src = source->source;
1316 tspp_set_tsif_mode(channel, source->mode);
1317 tspp_set_signal_inversion(channel, source->clk_inverse,
Liron Kuch72b78552012-10-30 17:47:50 +02001318 source->data_inverse, source->sync_inverse,
1319 source->enable_inverse);
Joel Nider5556a852011-10-16 10:52:13 +02001320
Hamad Kadmany92705b32012-10-23 14:15:41 +02001321 switch (source->source) {
Joel Nider5556a852011-10-16 10:52:13 +02001322 case TSPP_SOURCE_TSIF0:
Liron Kuch275c0b32013-02-10 15:19:32 +02001323 if (tspp_config_gpios(pdev, channel->src, 1) != 0) {
1324 pr_err("tspp: error enabling tsif0 GPIOs\n");
1325 return -EBUSY;
1326 }
Joel Nider5556a852011-10-16 10:52:13 +02001327 /* make sure TSIF0 is running & enabled */
1328 if (tspp_start_tsif(&pdev->tsif[0]) != 0) {
1329 pr_err("tspp: error starting tsif0");
Joel Nider435ad8e2011-12-14 16:53:30 +02001330 return -EBUSY;
Joel Nider5556a852011-10-16 10:52:13 +02001331 }
Liron Kucha7b49ae2013-02-14 16:26:38 +02001332 if (pdev->tsif[0].ref_count == 1) {
1333 val = readl_relaxed(pdev->base + TSPP_CONTROL);
1334 writel_relaxed(val & ~TSPP_CONTROL_TSP_TSIF0_SRC_DIS,
1335 pdev->base + TSPP_CONTROL);
1336 wmb();
1337 }
Joel Nider5556a852011-10-16 10:52:13 +02001338 break;
1339 case TSPP_SOURCE_TSIF1:
Liron Kuch275c0b32013-02-10 15:19:32 +02001340 if (tspp_config_gpios(pdev, channel->src, 1) != 0) {
1341 pr_err("tspp: error enabling tsif1 GPIOs\n");
1342 return -EBUSY;
1343 }
Joel Nider5556a852011-10-16 10:52:13 +02001344 /* make sure TSIF1 is running & enabled */
1345 if (tspp_start_tsif(&pdev->tsif[1]) != 0) {
1346 pr_err("tspp: error starting tsif1");
Joel Nider435ad8e2011-12-14 16:53:30 +02001347 return -EBUSY;
Joel Nider5556a852011-10-16 10:52:13 +02001348 }
Liron Kucha7b49ae2013-02-14 16:26:38 +02001349 if (pdev->tsif[1].ref_count == 1) {
1350 val = readl_relaxed(pdev->base + TSPP_CONTROL);
1351 writel_relaxed(val & ~TSPP_CONTROL_TSP_TSIF1_SRC_DIS,
1352 pdev->base + TSPP_CONTROL);
1353 wmb();
1354 }
Joel Nider5556a852011-10-16 10:52:13 +02001355 break;
1356 case TSPP_SOURCE_MEM:
1357 break;
1358 default:
Hamad Kadmany92705b32012-10-23 14:15:41 +02001359 pr_err("tspp: channel %i invalid source %i",
1360 channel->id, source->source);
Joel Nider435ad8e2011-12-14 16:53:30 +02001361 return -EBUSY;
Joel Nider5556a852011-10-16 10:52:13 +02001362 }
1363
Joel Nider5556a852011-10-16 10:52:13 +02001364 return 0;
1365}
1366EXPORT_SYMBOL(tspp_open_stream);
1367
Liron Kuch72b78552012-10-30 17:47:50 +02001368/**
1369 * tspp_close_stream - close a TSPP stream.
1370 *
1371 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1372 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
1373 *
1374 * Return error status
1375 *
1376 */
Joel Nider435ad8e2011-12-14 16:53:30 +02001377int tspp_close_stream(u32 dev, u32 channel_id)
Joel Nider5556a852011-10-16 10:52:13 +02001378{
1379 u32 val;
Liron Kucha7b49ae2013-02-14 16:26:38 +02001380 u32 prev_ref_count;
Joel Nider5556a852011-10-16 10:52:13 +02001381 struct tspp_device *pdev;
Joel Nider435ad8e2011-12-14 16:53:30 +02001382 struct tspp_channel *channel;
Joel Nider5556a852011-10-16 10:52:13 +02001383
Joel Nider435ad8e2011-12-14 16:53:30 +02001384 if (channel_id >= TSPP_NUM_CHANNELS) {
1385 pr_err("tspp: channel id out of range");
1386 return -ECHRNG;
1387 }
1388 pdev = tspp_find_by_id(dev);
1389 if (!pdev) {
1390 pr_err("tspp_cs: can't find device %i", dev);
1391 return -EBUSY;
1392 }
1393 channel = &pdev->channels[channel_id];
Joel Nider5556a852011-10-16 10:52:13 +02001394
1395 switch (channel->src) {
1396 case TSPP_SOURCE_TSIF0:
Liron Kucha7b49ae2013-02-14 16:26:38 +02001397 prev_ref_count = pdev->tsif[0].ref_count;
Joel Nider5556a852011-10-16 10:52:13 +02001398 tspp_stop_tsif(&pdev->tsif[0]);
Liron Kuch275c0b32013-02-10 15:19:32 +02001399 if (tspp_config_gpios(pdev, channel->src, 0) != 0)
1400 pr_err("tspp: error disabling tsif0 GPIOs\n");
1401
Liron Kucha7b49ae2013-02-14 16:26:38 +02001402 if (prev_ref_count == 1) {
1403 val = readl_relaxed(pdev->base + TSPP_CONTROL);
1404 writel_relaxed(val | TSPP_CONTROL_TSP_TSIF0_SRC_DIS,
1405 pdev->base + TSPP_CONTROL);
1406 wmb();
1407 }
Joel Nider5556a852011-10-16 10:52:13 +02001408 break;
1409 case TSPP_SOURCE_TSIF1:
Liron Kucha7b49ae2013-02-14 16:26:38 +02001410 prev_ref_count = pdev->tsif[1].ref_count;
Joel Nider5556a852011-10-16 10:52:13 +02001411 tspp_stop_tsif(&pdev->tsif[1]);
Liron Kuch275c0b32013-02-10 15:19:32 +02001412 if (tspp_config_gpios(pdev, channel->src, 0) != 0)
1413 pr_err("tspp: error disabling tsif0 GPIOs\n");
1414
Liron Kucha7b49ae2013-02-14 16:26:38 +02001415 if (prev_ref_count == 1) {
1416 val = readl_relaxed(pdev->base + TSPP_CONTROL);
1417 writel_relaxed(val | TSPP_CONTROL_TSP_TSIF1_SRC_DIS,
1418 pdev->base + TSPP_CONTROL);
1419 wmb();
1420 }
Joel Nider5556a852011-10-16 10:52:13 +02001421 break;
1422 case TSPP_SOURCE_MEM:
1423 break;
1424 case TSPP_SOURCE_NONE:
1425 break;
1426 }
1427
Joel Nider435ad8e2011-12-14 16:53:30 +02001428 channel->src = TSPP_SOURCE_NONE;
Joel Nider5556a852011-10-16 10:52:13 +02001429 return 0;
1430}
1431EXPORT_SYMBOL(tspp_close_stream);
1432
Liron Kuch72b78552012-10-30 17:47:50 +02001433/**
1434 * tspp_open_channel - open a TSPP channel.
1435 *
1436 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1437 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
1438 *
1439 * Return error status
1440 *
1441 */
Joel Nider435ad8e2011-12-14 16:53:30 +02001442int tspp_open_channel(u32 dev, u32 channel_id)
Joel Nider5556a852011-10-16 10:52:13 +02001443{
1444 int rc = 0;
Joel Nider435ad8e2011-12-14 16:53:30 +02001445 struct sps_connect *config;
1446 struct sps_register_event *event;
1447 struct tspp_channel *channel;
1448 struct tspp_device *pdev;
1449
1450 if (channel_id >= TSPP_NUM_CHANNELS) {
1451 pr_err("tspp: channel id out of range");
1452 return -ECHRNG;
1453 }
1454 pdev = tspp_find_by_id(dev);
1455 if (!pdev) {
1456 pr_err("tspp_oc: can't find device %i", dev);
1457 return -ENODEV;
1458 }
1459 channel = &pdev->channels[channel_id];
Joel Nider5556a852011-10-16 10:52:13 +02001460
1461 if (channel->used) {
1462 pr_err("tspp channel already in use");
Joel Nider435ad8e2011-12-14 16:53:30 +02001463 return -EBUSY;
Joel Nider5556a852011-10-16 10:52:13 +02001464 }
1465
Joel Nider435ad8e2011-12-14 16:53:30 +02001466 config = &channel->config;
1467 event = &channel->event;
1468
1469 /* start the clocks if needed */
Liron Kuch59339922013-01-01 18:29:47 +02001470 if (tspp_channels_in_use(pdev) == 0) {
Liron Kuch605cc122013-02-21 14:25:57 +02001471 rc = tspp_clock_start(pdev);
1472 if (rc)
1473 return rc;
1474
Joel Nider435ad8e2011-12-14 16:53:30 +02001475 wake_lock(&pdev->wake_lock);
Liron Kuch59339922013-01-01 18:29:47 +02001476 }
Joel Nider435ad8e2011-12-14 16:53:30 +02001477
Joel Nider5556a852011-10-16 10:52:13 +02001478 /* mark it as used */
1479 channel->used = 1;
1480
1481 /* start the bam */
1482 channel->pipe = sps_alloc_endpoint();
1483 if (channel->pipe == 0) {
1484 pr_err("tspp: error allocating endpoint");
1485 rc = -ENOMEM;
1486 goto err_sps_alloc;
1487 }
1488
1489 /* get default configuration */
1490 sps_get_config(channel->pipe, config);
1491
Joel Nider435ad8e2011-12-14 16:53:30 +02001492 config->source = pdev->bam_handle;
Joel Nider5556a852011-10-16 10:52:13 +02001493 config->destination = SPS_DEV_HANDLE_MEM;
1494 config->mode = SPS_MODE_SRC;
Joel Nider435ad8e2011-12-14 16:53:30 +02001495 config->options =
1496 SPS_O_AUTO_ENABLE | /* connection is auto-enabled */
1497 SPS_O_STREAMING | /* streaming mode */
1498 SPS_O_DESC_DONE | /* interrupt on end of descriptor */
Hamad Kadmany81cee052012-11-29 14:15:57 +02001499 SPS_O_ACK_TRANSFERS | /* must use sps_get_iovec() */
1500 SPS_O_HYBRID; /* Read actual descriptors in sps_get_iovec() */
Joel Nider5556a852011-10-16 10:52:13 +02001501 config->src_pipe_index = channel->id;
1502 config->desc.size =
Hamad Kadmany81cee052012-11-29 14:15:57 +02001503 TSPP_SPS_DESCRIPTOR_COUNT * SPS_DESCRIPTOR_SIZE;
Joel Nider5556a852011-10-16 10:52:13 +02001504 config->desc.base = dma_alloc_coherent(NULL,
1505 config->desc.size,
1506 &config->desc.phys_base,
1507 GFP_KERNEL);
1508 if (config->desc.base == 0) {
1509 pr_err("tspp: error allocating sps descriptors");
1510 rc = -ENOMEM;
1511 goto err_desc_alloc;
1512 }
1513
1514 memset(config->desc.base, 0, config->desc.size);
1515
1516 rc = sps_connect(channel->pipe, config);
1517 if (rc) {
1518 pr_err("tspp: error connecting bam");
1519 goto err_connect;
1520 }
1521
1522 event->mode = SPS_TRIGGER_CALLBACK;
Joel Nider435ad8e2011-12-14 16:53:30 +02001523 event->options = SPS_O_DESC_DONE;
Joel Nider5556a852011-10-16 10:52:13 +02001524 event->callback = tspp_sps_complete_cb;
1525 event->xfer_done = NULL;
Joel Nider435ad8e2011-12-14 16:53:30 +02001526 event->user = pdev;
Joel Nider5556a852011-10-16 10:52:13 +02001527
1528 rc = sps_register_event(channel->pipe, event);
1529 if (rc) {
1530 pr_err("tspp: error registering event");
1531 goto err_event;
1532 }
1533
Hamad Kadmany81cee052012-11-29 14:15:57 +02001534 init_timer(&channel->expiration_timer);
1535 channel->expiration_timer.function = tspp_expiration_timer;
1536 channel->expiration_timer.data = (unsigned long)pdev;
1537 channel->expiration_timer.expires = 0xffffffffL;
1538
Joel Nider435ad8e2011-12-14 16:53:30 +02001539 rc = pm_runtime_get(&pdev->pdev->dev);
Joel Nider5556a852011-10-16 10:52:13 +02001540 if (rc < 0) {
Joel Nider435ad8e2011-12-14 16:53:30 +02001541 dev_err(&pdev->pdev->dev,
Joel Nider5556a852011-10-16 10:52:13 +02001542 "Runtime PM: Unable to wake up tspp device, rc = %d",
1543 rc);
1544 }
Joel Nider5556a852011-10-16 10:52:13 +02001545 return 0;
1546
1547err_event:
1548 sps_disconnect(channel->pipe);
1549err_connect:
1550 dma_free_coherent(NULL, config->desc.size, config->desc.base,
1551 config->desc.phys_base);
1552err_desc_alloc:
1553 sps_free_endpoint(channel->pipe);
1554err_sps_alloc:
1555 return rc;
1556}
1557EXPORT_SYMBOL(tspp_open_channel);
1558
Liron Kuch72b78552012-10-30 17:47:50 +02001559/**
1560 * tspp_close_channel - close a TSPP channel.
1561 *
1562 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1563 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
1564 *
1565 * Return error status
1566 *
1567 */
Joel Nider435ad8e2011-12-14 16:53:30 +02001568int tspp_close_channel(u32 dev, u32 channel_id)
Joel Nider5556a852011-10-16 10:52:13 +02001569{
1570 int i;
1571 int id;
Liron Kuch4ed3bf62013-03-28 09:44:42 +02001572 int table_idx;
Joel Nider5556a852011-10-16 10:52:13 +02001573 u32 val;
Joel Nider5556a852011-10-16 10:52:13 +02001574
Joel Nider435ad8e2011-12-14 16:53:30 +02001575 struct sps_connect *config;
1576 struct tspp_device *pdev;
1577 struct tspp_channel *channel;
Joel Nider435ad8e2011-12-14 16:53:30 +02001578
1579 if (channel_id >= TSPP_NUM_CHANNELS) {
1580 pr_err("tspp: channel id out of range");
1581 return -ECHRNG;
1582 }
1583 pdev = tspp_find_by_id(dev);
1584 if (!pdev) {
1585 pr_err("tspp_close: can't find device %i", dev);
1586 return -ENODEV;
1587 }
1588 channel = &pdev->channels[channel_id];
1589
1590 /* if the channel is not used, we are done */
1591 if (!channel->used)
1592 return 0;
1593
Hamad Kadmany81cee052012-11-29 14:15:57 +02001594 if (channel->expiration_period_ms)
1595 del_timer(&channel->expiration_timer);
1596
Joel Nider435ad8e2011-12-14 16:53:30 +02001597 channel->notifier = NULL;
1598 channel->notify_data = NULL;
Hamad Kadmany81cee052012-11-29 14:15:57 +02001599 channel->expiration_period_ms = 0;
Joel Nider435ad8e2011-12-14 16:53:30 +02001600
1601 config = &channel->config;
1602 pdev = channel->pdev;
Joel Nider5556a852011-10-16 10:52:13 +02001603
1604 /* disable pipe (channel) */
1605 val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
1606 writel_relaxed(val | channel->id, pdev->base + TSPP_PS_DISABLE);
1607 wmb();
1608
1609 /* unregister all filters for this channel */
Liron Kuch4ed3bf62013-03-28 09:44:42 +02001610 for (table_idx = 0; table_idx < TSPP_FILTER_TABLES; table_idx++) {
1611 for (i = 0; i < TSPP_NUM_PRIORITIES; i++) {
1612 struct tspp_pid_filter *filter =
1613 &pdev->filters[table_idx]->filter[i];
1614 id = FILTER_GET_PIPE_NUMBER0(filter);
1615 if (id == channel->id) {
1616 if (FILTER_HAS_ENCRYPTION(filter))
1617 tspp_free_key_entry(
1618 FILTER_GET_KEY_NUMBER(filter));
1619 filter->config = 0;
1620 filter->filter = 0;
1621 }
Joel Nider5556a852011-10-16 10:52:13 +02001622 }
1623 }
1624 channel->filter_count = 0;
1625
Joel Nider5556a852011-10-16 10:52:13 +02001626 /* disconnect the bam */
1627 if (sps_disconnect(channel->pipe) != 0)
1628 pr_warn("tspp: Error freeing sps endpoint (%i)", channel->id);
1629
1630 /* destroy the buffers */
1631 dma_free_coherent(NULL, config->desc.size, config->desc.base,
1632 config->desc.phys_base);
1633
Liron Kuch72b78552012-10-30 17:47:50 +02001634 tspp_destroy_buffers(channel_id, channel);
Hamad Kadmany090709b2013-01-06 12:08:13 +02001635 if (channel->dma_pool) {
1636 dma_pool_destroy(channel->dma_pool);
1637 channel->dma_pool = NULL;
1638 }
Liron Kuch72b78552012-10-30 17:47:50 +02001639
1640 channel->src = TSPP_SOURCE_NONE;
1641 channel->mode = TSPP_MODE_DISABLED;
1642 channel->memfree = NULL;
1643 channel->user_info = NULL;
Joel Nider5556a852011-10-16 10:52:13 +02001644 channel->buffer_count = 0;
Joel Nider435ad8e2011-12-14 16:53:30 +02001645 channel->data = NULL;
1646 channel->read = NULL;
1647 channel->waiting = NULL;
1648 channel->locked = NULL;
1649 channel->used = 0;
Joel Nider5556a852011-10-16 10:52:13 +02001650
Liron Kuch59339922013-01-01 18:29:47 +02001651 if (tspp_channels_in_use(pdev) == 0) {
Joel Nider435ad8e2011-12-14 16:53:30 +02001652 wake_unlock(&pdev->wake_lock);
Liron Kuch59339922013-01-01 18:29:47 +02001653 tspp_clock_stop(pdev);
1654 }
Joel Nider435ad8e2011-12-14 16:53:30 +02001655
Liron Kuch605cc122013-02-21 14:25:57 +02001656 pm_runtime_put(&pdev->pdev->dev);
1657
Joel Nider5556a852011-10-16 10:52:13 +02001658 return 0;
1659}
1660EXPORT_SYMBOL(tspp_close_channel);
1661
Liron Kuch72b78552012-10-30 17:47:50 +02001662/**
Hamad Kadmany6d2a9c72013-01-31 14:49:20 +02001663 * tspp_get_ref_clk_counter - return the TSIF clock reference (TCR) counter.
1664 *
1665 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1666 * @source: The TSIF source from which the counter should be read
1667 * @tcr_counter: the value of TCR counter
1668 *
1669 * Return error status
1670 *
1671 * TCR increments at a rate equal to 27 MHz/256 = 105.47 kHz.
1672 * If source is neither TSIF 0 or TSIF1 0 is returned.
1673 */
1674int tspp_get_ref_clk_counter(u32 dev, enum tspp_source source, u32 *tcr_counter)
1675{
1676 struct tspp_device *pdev;
1677 struct tspp_tsif_device *tsif_device;
1678
1679 if (!tcr_counter)
1680 return -EINVAL;
1681
1682 pdev = tspp_find_by_id(dev);
1683 if (!pdev) {
1684 pr_err("tspp_get_ref_clk_counter: can't find device %i\n", dev);
1685 return -ENODEV;
1686 }
1687
1688 switch (source) {
1689 case TSPP_SOURCE_TSIF0:
1690 tsif_device = &pdev->tsif[0];
1691 break;
1692
1693 case TSPP_SOURCE_TSIF1:
1694 tsif_device = &pdev->tsif[1];
1695 break;
1696
1697 default:
1698 tsif_device = NULL;
1699 break;
1700 }
1701
1702 if (tsif_device && tsif_device->ref_count)
1703 *tcr_counter = ioread32(tsif_device->base + TSIF_CLK_REF_OFF);
1704 else
1705 *tcr_counter = 0;
1706
1707 return 0;
1708}
1709EXPORT_SYMBOL(tspp_get_ref_clk_counter);
1710
1711/**
Liron Kuch72b78552012-10-30 17:47:50 +02001712 * tspp_add_filter - add a TSPP filter to a channel.
1713 *
1714 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1715 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
1716 * @filter: TSPP filter parameters
1717 *
1718 * Return error status
1719 *
1720 */
Joel Nider435ad8e2011-12-14 16:53:30 +02001721int tspp_add_filter(u32 dev, u32 channel_id,
Joel Nider5556a852011-10-16 10:52:13 +02001722 struct tspp_filter *filter)
1723{
Liron Kuch72b78552012-10-30 17:47:50 +02001724 int i, rc;
Joel Nider5556a852011-10-16 10:52:13 +02001725 int other_channel;
1726 int entry;
1727 u32 val, pid, enabled;
Joel Nider435ad8e2011-12-14 16:53:30 +02001728 struct tspp_device *pdev;
Joel Nider5556a852011-10-16 10:52:13 +02001729 struct tspp_pid_filter p;
Joel Nider435ad8e2011-12-14 16:53:30 +02001730 struct tspp_channel *channel;
Joel Nider5556a852011-10-16 10:52:13 +02001731
Joel Nider435ad8e2011-12-14 16:53:30 +02001732 TSPP_DEBUG("tspp: add filter");
1733 if (channel_id >= TSPP_NUM_CHANNELS) {
1734 pr_err("tspp: channel id out of range");
1735 return -ECHRNG;
1736 }
1737 pdev = tspp_find_by_id(dev);
1738 if (!pdev) {
1739 pr_err("tspp_add: can't find device %i", dev);
1740 return -ENODEV;
1741 }
1742
1743 channel = &pdev->channels[channel_id];
1744
Joel Nider5556a852011-10-16 10:52:13 +02001745 if (filter->source > TSPP_SOURCE_MEM) {
1746 pr_err("tspp invalid source");
Joel Nider435ad8e2011-12-14 16:53:30 +02001747 return -ENOSR;
Joel Nider5556a852011-10-16 10:52:13 +02001748 }
1749
1750 if (filter->priority >= TSPP_NUM_PRIORITIES) {
1751 pr_err("tspp invalid source");
Joel Nider435ad8e2011-12-14 16:53:30 +02001752 return -ENOSR;
Joel Nider5556a852011-10-16 10:52:13 +02001753 }
1754
Liron Kuch72b78552012-10-30 17:47:50 +02001755 channel->mode = filter->mode;
1756 /*
1757 * if buffers are already allocated, verify they fulfil
1758 * the alignment requirements.
1759 */
1760 if ((channel->buffer_count > 0) &&
1761 (!tspp_is_buffer_size_aligned(channel->buffer_size, channel->mode)))
1762 pr_warn("tspp: buffers allocated with incorrect alignment\n");
Joel Nider5556a852011-10-16 10:52:13 +02001763
1764 if (filter->mode == TSPP_MODE_PES) {
1765 for (i = 0; i < TSPP_NUM_PRIORITIES; i++) {
1766 struct tspp_pid_filter *tspp_filter =
Joel Nider435ad8e2011-12-14 16:53:30 +02001767 &pdev->filters[channel->src]->filter[i];
Joel Nider5556a852011-10-16 10:52:13 +02001768 pid = FILTER_GET_PIPE_PID((tspp_filter));
1769 enabled = FILTER_GET_PIPE_PROCESS0(tspp_filter);
1770 if (enabled && (pid == filter->pid)) {
1771 other_channel =
1772 FILTER_GET_PIPE_NUMBER0(tspp_filter);
1773 pr_err("tspp: pid 0x%x already in use by channel %i",
1774 filter->pid, other_channel);
Joel Nider435ad8e2011-12-14 16:53:30 +02001775 return -EBADSLT;
Joel Nider5556a852011-10-16 10:52:13 +02001776 }
1777 }
1778 }
1779
1780 /* make sure this priority is not already in use */
1781 enabled = FILTER_GET_PIPE_PROCESS0(
Joel Nider435ad8e2011-12-14 16:53:30 +02001782 (&(pdev->filters[channel->src]->filter[filter->priority])));
Joel Nider5556a852011-10-16 10:52:13 +02001783 if (enabled) {
1784 pr_err("tspp: filter priority %i source %i is already enabled\n",
1785 filter->priority, channel->src);
Joel Nider435ad8e2011-12-14 16:53:30 +02001786 return -ENOSR;
Joel Nider5556a852011-10-16 10:52:13 +02001787 }
1788
1789 if (channel->mode == TSPP_MODE_PES) {
1790 /* if we are already processing in PES mode, disable pipe
1791 (channel) and filter to be updated */
1792 val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
1793 writel_relaxed(val | (1 << channel->id),
1794 pdev->base + TSPP_PS_DISABLE);
1795 wmb();
1796 }
1797
1798 /* update entry */
1799 p.filter = 0;
Joel Nider435ad8e2011-12-14 16:53:30 +02001800 p.config = FILTER_TRANS_END_DISABLE;
Joel Nider5556a852011-10-16 10:52:13 +02001801 FILTER_SET_PIPE_PROCESS0((&p), filter->mode);
1802 FILTER_SET_PIPE_PID((&p), filter->pid);
1803 FILTER_SET_PID_MASK((&p), filter->mask);
1804 FILTER_SET_PIPE_NUMBER0((&p), channel->id);
1805 FILTER_SET_PIPE_PROCESS1((&p), TSPP_MODE_DISABLED);
1806 if (filter->decrypt) {
1807 entry = tspp_get_key_entry();
1808 if (entry == -1) {
1809 pr_err("tspp: no more keys available!");
1810 } else {
1811 p.config |= FILTER_DECRYPT;
1812 FILTER_SET_KEY_NUMBER((&p), entry);
1813 }
1814 }
Joel Nider5556a852011-10-16 10:52:13 +02001815
Joel Nider435ad8e2011-12-14 16:53:30 +02001816 pdev->filters[channel->src]->
Joel Nider5556a852011-10-16 10:52:13 +02001817 filter[filter->priority].config = p.config;
Joel Nider435ad8e2011-12-14 16:53:30 +02001818 pdev->filters[channel->src]->
Joel Nider5556a852011-10-16 10:52:13 +02001819 filter[filter->priority].filter = p.filter;
1820
Liron Kuch72b78552012-10-30 17:47:50 +02001821 /*
1822 * allocate buffers if needed (i.e. if user did has not already called
1823 * tspp_allocate_buffers() explicitly).
1824 */
1825 if (channel->buffer_count == 0) {
1826 channel->buffer_size =
Hamad Kadmany090709b2013-01-06 12:08:13 +02001827 tspp_align_buffer_size_by_mode(channel->buffer_size,
Liron Kuch72b78552012-10-30 17:47:50 +02001828 channel->mode);
1829 rc = tspp_allocate_buffers(dev, channel->id,
1830 channel->max_buffers,
1831 channel->buffer_size,
1832 channel->int_freq, NULL, NULL, NULL);
1833 if (rc != 0) {
1834 pr_err("tspp: tspp_allocate_buffers failed\n");
1835 return rc;
1836 }
Joel Nider435ad8e2011-12-14 16:53:30 +02001837 }
1838
Joel Nider5556a852011-10-16 10:52:13 +02001839 /* reenable pipe */
1840 val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
1841 writel_relaxed(val & ~(1 << channel->id), pdev->base + TSPP_PS_DISABLE);
1842 wmb();
1843 val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
1844
Joel Nider5556a852011-10-16 10:52:13 +02001845 channel->filter_count++;
1846
1847 return 0;
1848}
1849EXPORT_SYMBOL(tspp_add_filter);
1850
Liron Kuch72b78552012-10-30 17:47:50 +02001851/**
1852 * tspp_remove_filter - remove a TSPP filter from a channel.
1853 *
1854 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1855 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
1856 * @filter: TSPP filter parameters
1857 *
1858 * Return error status
1859 *
1860 */
Joel Nider435ad8e2011-12-14 16:53:30 +02001861int tspp_remove_filter(u32 dev, u32 channel_id,
Joel Nider5556a852011-10-16 10:52:13 +02001862 struct tspp_filter *filter)
1863{
1864 int entry;
1865 u32 val;
Joel Nider435ad8e2011-12-14 16:53:30 +02001866 struct tspp_device *pdev;
1867 int src;
1868 struct tspp_pid_filter *tspp_filter;
1869 struct tspp_channel *channel;
1870
1871 if (channel_id >= TSPP_NUM_CHANNELS) {
1872 pr_err("tspp: channel id out of range");
1873 return -ECHRNG;
1874 }
1875 pdev = tspp_find_by_id(dev);
1876 if (!pdev) {
1877 pr_err("tspp_remove: can't find device %i", dev);
1878 return -ENODEV;
1879 }
1880 channel = &pdev->channels[channel_id];
1881
1882 src = channel->src;
1883 tspp_filter = &(pdev->filters[src]->filter[filter->priority]);
Joel Nider5556a852011-10-16 10:52:13 +02001884
1885 /* disable pipe (channel) */
1886 val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
1887 writel_relaxed(val | channel->id, pdev->base + TSPP_PS_DISABLE);
1888 wmb();
1889
1890 /* update data keys */
1891 if (tspp_filter->config & FILTER_DECRYPT) {
1892 entry = FILTER_GET_KEY_NUMBER(tspp_filter);
1893 tspp_free_key_entry(entry);
1894 }
1895
1896 /* update pid table */
1897 tspp_filter->config = 0;
1898 tspp_filter->filter = 0;
1899
1900 channel->filter_count--;
1901
1902 /* reenable pipe */
1903 val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
1904 writel_relaxed(val & ~(1 << channel->id),
1905 pdev->base + TSPP_PS_DISABLE);
1906 wmb();
1907 val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
1908
1909 return 0;
1910}
1911EXPORT_SYMBOL(tspp_remove_filter);
1912
Liron Kuch72b78552012-10-30 17:47:50 +02001913/**
1914 * tspp_set_key - set TSPP key in key table.
1915 *
1916 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1917 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
1918 * @key: TSPP key parameters
1919 *
1920 * Return error status
1921 *
1922 */
Joel Nider435ad8e2011-12-14 16:53:30 +02001923int tspp_set_key(u32 dev, u32 channel_id, struct tspp_key *key)
Joel Nider5556a852011-10-16 10:52:13 +02001924{
1925 int i;
1926 int id;
1927 int key_index;
1928 int data;
Joel Nider435ad8e2011-12-14 16:53:30 +02001929 struct tspp_channel *channel;
1930 struct tspp_device *pdev;
1931
1932 if (channel_id >= TSPP_NUM_CHANNELS) {
1933 pr_err("tspp: channel id out of range");
1934 return -ECHRNG;
1935 }
1936 pdev = tspp_find_by_id(dev);
1937 if (!pdev) {
1938 pr_err("tspp_set: can't find device %i", dev);
1939 return -ENODEV;
1940 }
1941 channel = &pdev->channels[channel_id];
Joel Nider5556a852011-10-16 10:52:13 +02001942
1943 /* read the key index used by this channel */
1944 for (i = 0; i < TSPP_NUM_PRIORITIES; i++) {
1945 struct tspp_pid_filter *tspp_filter =
Joel Nider435ad8e2011-12-14 16:53:30 +02001946 &(pdev->filters[channel->src]->filter[i]);
Joel Nider5556a852011-10-16 10:52:13 +02001947 id = FILTER_GET_PIPE_NUMBER0(tspp_filter);
1948 if (id == channel->id) {
1949 if (FILTER_HAS_ENCRYPTION(tspp_filter)) {
1950 key_index = FILTER_GET_KEY_NUMBER(tspp_filter);
1951 break;
1952 }
1953 }
1954 }
1955 if (i == TSPP_NUM_PRIORITIES) {
1956 pr_err("tspp: no encryption on this channel");
Joel Nider435ad8e2011-12-14 16:53:30 +02001957 return -ENOKEY;
Joel Nider5556a852011-10-16 10:52:13 +02001958 }
1959
1960 if (key->parity == TSPP_KEY_PARITY_EVEN) {
Joel Nider435ad8e2011-12-14 16:53:30 +02001961 pdev->tspp_key_table->entry[key_index].even_lsb = key->lsb;
1962 pdev->tspp_key_table->entry[key_index].even_msb = key->msb;
Joel Nider5556a852011-10-16 10:52:13 +02001963 } else {
Joel Nider435ad8e2011-12-14 16:53:30 +02001964 pdev->tspp_key_table->entry[key_index].odd_lsb = key->lsb;
1965 pdev->tspp_key_table->entry[key_index].odd_msb = key->msb;
Joel Nider5556a852011-10-16 10:52:13 +02001966 }
1967 data = readl_relaxed(channel->pdev->base + TSPP_KEY_VALID);
1968
1969 return 0;
1970}
1971EXPORT_SYMBOL(tspp_set_key);
1972
Liron Kuch72b78552012-10-30 17:47:50 +02001973/**
1974 * tspp_register_notification - register TSPP channel notification function.
1975 *
1976 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1977 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
1978 * @pNotify: notification function
1979 * @userdata: user data to pass to notification function
1980 * @timer_ms: notification for partially filled buffers
1981 *
1982 * Return error status
1983 *
1984 */
Joel Nider435ad8e2011-12-14 16:53:30 +02001985int tspp_register_notification(u32 dev, u32 channel_id,
1986 tspp_notifier *pNotify, void *userdata, u32 timer_ms)
Joel Nider5556a852011-10-16 10:52:13 +02001987{
Joel Nider435ad8e2011-12-14 16:53:30 +02001988 struct tspp_channel *channel;
1989 struct tspp_device *pdev;
Joel Nider5556a852011-10-16 10:52:13 +02001990
Joel Nider435ad8e2011-12-14 16:53:30 +02001991 if (channel_id >= TSPP_NUM_CHANNELS) {
1992 pr_err("tspp: channel id out of range");
1993 return -ECHRNG;
1994 }
1995 pdev = tspp_find_by_id(dev);
1996 if (!pdev) {
1997 pr_err("tspp_reg: can't find device %i", dev);
1998 return -ENODEV;
1999 }
2000 channel = &pdev->channels[channel_id];
2001 channel->notifier = pNotify;
2002 channel->notify_data = userdata;
Hamad Kadmany81cee052012-11-29 14:15:57 +02002003 channel->expiration_period_ms = timer_ms;
2004
Joel Nider5556a852011-10-16 10:52:13 +02002005 return 0;
2006}
Joel Nider435ad8e2011-12-14 16:53:30 +02002007EXPORT_SYMBOL(tspp_register_notification);
Joel Nider5556a852011-10-16 10:52:13 +02002008
Liron Kuch72b78552012-10-30 17:47:50 +02002009/**
2010 * tspp_unregister_notification - unregister TSPP channel notification function.
2011 *
2012 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
2013 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
2014 *
2015 * Return error status
2016 *
2017 */
Joel Nider435ad8e2011-12-14 16:53:30 +02002018int tspp_unregister_notification(u32 dev, u32 channel_id)
Joel Nider5556a852011-10-16 10:52:13 +02002019{
Joel Nider435ad8e2011-12-14 16:53:30 +02002020 struct tspp_channel *channel;
2021 struct tspp_device *pdev;
Joel Nider5556a852011-10-16 10:52:13 +02002022
Joel Nider435ad8e2011-12-14 16:53:30 +02002023 if (channel_id >= TSPP_NUM_CHANNELS) {
2024 pr_err("tspp: channel id out of range");
2025 return -ECHRNG;
2026 }
2027 pdev = tspp_find_by_id(dev);
2028 if (!pdev) {
2029 pr_err("tspp_unreg: can't find device %i", dev);
2030 return -ENODEV;
2031 }
2032 channel = &pdev->channels[channel_id];
2033 channel->notifier = NULL;
2034 channel->notify_data = 0;
Joel Nider5556a852011-10-16 10:52:13 +02002035 return 0;
2036}
Joel Nider435ad8e2011-12-14 16:53:30 +02002037EXPORT_SYMBOL(tspp_unregister_notification);
Joel Nider5556a852011-10-16 10:52:13 +02002038
Liron Kuch72b78552012-10-30 17:47:50 +02002039/**
2040 * tspp_get_buffer - get TSPP data buffer.
2041 *
2042 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
2043 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
2044 *
2045 * Return error status
2046 *
2047 */
Joel Nider435ad8e2011-12-14 16:53:30 +02002048const struct tspp_data_descriptor *tspp_get_buffer(u32 dev, u32 channel_id)
Joel Nider5556a852011-10-16 10:52:13 +02002049{
Joel Nider435ad8e2011-12-14 16:53:30 +02002050 struct tspp_mem_buffer *buffer;
2051 struct tspp_channel *channel;
2052 struct tspp_device *pdev;
Joel Nider5556a852011-10-16 10:52:13 +02002053
Joel Nider435ad8e2011-12-14 16:53:30 +02002054 if (channel_id >= TSPP_NUM_CHANNELS) {
2055 pr_err("tspp: channel id out of range");
2056 return NULL;
2057 }
2058 pdev = tspp_find_by_id(dev);
2059 if (!pdev) {
2060 pr_err("tspp_get: can't find device %i", dev);
2061 return NULL;
2062 }
2063 channel = &pdev->channels[channel_id];
Joel Nider5556a852011-10-16 10:52:13 +02002064
Joel Nider435ad8e2011-12-14 16:53:30 +02002065 if (!channel->read) {
2066 pr_warn("tspp: no buffer to get on channel %i!",
2067 channel->id);
2068 return NULL;
2069 }
2070
2071 buffer = channel->read;
2072 /* see if we have any buffers ready to read */
2073 if (buffer->state != TSPP_BUF_STATE_DATA)
2074 return 0;
2075
2076 if (buffer->state == TSPP_BUF_STATE_DATA) {
2077 /* mark the buffer as busy */
2078 buffer->state = TSPP_BUF_STATE_LOCKED;
2079
2080 /* increment the pointer along the list */
2081 channel->read = channel->read->next;
2082 }
2083
2084 return &buffer->desc;
2085}
2086EXPORT_SYMBOL(tspp_get_buffer);
2087
Liron Kuch72b78552012-10-30 17:47:50 +02002088/**
2089 * tspp_release_buffer - release TSPP data buffer back to TSPP.
2090 *
2091 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
2092 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
2093 * @descriptor_id: buffer descriptor ID
2094 *
2095 * Return error status
2096 *
2097 */
Joel Nider435ad8e2011-12-14 16:53:30 +02002098int tspp_release_buffer(u32 dev, u32 channel_id, u32 descriptor_id)
2099{
2100 int i, found = 0;
2101 struct tspp_mem_buffer *buffer;
2102 struct tspp_channel *channel;
2103 struct tspp_device *pdev;
2104
2105 if (channel_id >= TSPP_NUM_CHANNELS) {
2106 pr_err("tspp: channel id out of range");
2107 return -ECHRNG;
2108 }
2109 pdev = tspp_find_by_id(dev);
2110 if (!pdev) {
2111 pr_err("tspp: can't find device %i", dev);
2112 return -ENODEV;
2113 }
2114 channel = &pdev->channels[channel_id];
2115
2116 if (descriptor_id > channel->buffer_count)
2117 pr_warn("tspp: desc id looks weird 0x%08x", descriptor_id);
2118
2119 /* find the correct descriptor */
2120 buffer = channel->locked;
2121 for (i = 0; i < channel->buffer_count; i++) {
2122 if (buffer->desc.id == descriptor_id) {
2123 found = 1;
2124 break;
2125 }
2126 buffer = buffer->next;
2127 }
2128 channel->locked = channel->locked->next;
2129
2130 if (!found) {
2131 pr_err("tspp: cant find desc %i", descriptor_id);
2132 return -EINVAL;
2133 }
2134
2135 /* make sure the buffer is in the expected state */
2136 if (buffer->state != TSPP_BUF_STATE_LOCKED) {
2137 pr_err("tspp: buffer %i not locked", descriptor_id);
2138 return -EINVAL;
2139 }
2140 /* unlock the buffer and requeue it */
2141 buffer->state = TSPP_BUF_STATE_WAITING;
2142
2143 if (tspp_queue_buffer(channel, buffer))
2144 pr_warn("tspp: can't requeue buffer");
Joel Nider5556a852011-10-16 10:52:13 +02002145 return 0;
2146}
Joel Nider435ad8e2011-12-14 16:53:30 +02002147EXPORT_SYMBOL(tspp_release_buffer);
2148
Liron Kuch72b78552012-10-30 17:47:50 +02002149/**
2150 * tspp_allocate_buffers - allocate TSPP data buffers.
2151 *
2152 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
2153 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
2154 * @count: number of buffers to allocate
2155 * @size: size of each buffer to allocate
2156 * @int_freq: interrupt frequency
2157 * @alloc: user defined memory allocator function. Pass NULL for default.
2158 * @memfree: user defined memory free function. Pass NULL for default.
2159 * @user: user data to pass to the memory allocator/free function
2160 *
2161 * Return error status
2162 *
2163 * The user can optionally call this function explicitly to allocate the TSPP
2164 * data buffers. Alternatively, if the user did not call this function, it
2165 * is called implicitly by tspp_add_filter().
2166 */
2167int tspp_allocate_buffers(u32 dev, u32 channel_id, u32 count, u32 size,
2168 u32 int_freq, tspp_allocator *alloc,
2169 tspp_memfree *memfree, void *user)
Joel Nider435ad8e2011-12-14 16:53:30 +02002170{
2171 struct tspp_channel *channel;
2172 struct tspp_device *pdev;
2173 struct tspp_mem_buffer *last = NULL;
2174
2175 TSPP_DEBUG("tspp_allocate_buffers");
2176
2177 if (channel_id >= TSPP_NUM_CHANNELS) {
Liron Kuch72b78552012-10-30 17:47:50 +02002178 pr_err("%s: channel id out of range", __func__);
Joel Nider435ad8e2011-12-14 16:53:30 +02002179 return -ECHRNG;
2180 }
Liron Kuch72b78552012-10-30 17:47:50 +02002181
Joel Nider435ad8e2011-12-14 16:53:30 +02002182 pdev = tspp_find_by_id(dev);
2183 if (!pdev) {
Liron Kuch72b78552012-10-30 17:47:50 +02002184 pr_err("%s: can't find device %i", __func__, dev);
Joel Nider435ad8e2011-12-14 16:53:30 +02002185 return -ENODEV;
2186 }
Liron Kuch72b78552012-10-30 17:47:50 +02002187
2188 if (count < MIN_ACCEPTABLE_BUFFER_COUNT) {
2189 pr_err("%s: tspp requires a minimum of %i buffers\n",
2190 __func__, MIN_ACCEPTABLE_BUFFER_COUNT);
2191 return -EINVAL;
2192 }
2193
Joel Nider435ad8e2011-12-14 16:53:30 +02002194 channel = &pdev->channels[channel_id];
Hamad Kadmany090709b2013-01-06 12:08:13 +02002195
Liron Kuch72b78552012-10-30 17:47:50 +02002196 /* allow buffer allocation only if there was no previous buffer
2197 * allocation for this channel.
2198 */
2199 if (channel->buffer_count > 0) {
2200 pr_err("%s: buffers already allocated for channel %u",
2201 __func__, channel_id);
2202 return -EINVAL;
2203 }
Joel Nider435ad8e2011-12-14 16:53:30 +02002204
2205 channel->max_buffers = count;
2206
2207 /* set up interrupt frequency */
Liron Kuch72b78552012-10-30 17:47:50 +02002208 if (int_freq > channel->max_buffers) {
Joel Nider435ad8e2011-12-14 16:53:30 +02002209 int_freq = channel->max_buffers;
Liron Kuch72b78552012-10-30 17:47:50 +02002210 pr_warn("%s: setting interrupt frequency to %u\n",
2211 __func__, int_freq);
Joel Nider435ad8e2011-12-14 16:53:30 +02002212 }
Liron Kuch72b78552012-10-30 17:47:50 +02002213 channel->int_freq = int_freq;
2214 /*
2215 * it is the responsibility of the caller to tspp_allocate_buffers(),
2216 * whether it's the user or the driver, to make sure the size parameter
2217 * is compatible to the channel mode.
2218 */
2219 channel->buffer_size = size;
Joel Nider435ad8e2011-12-14 16:53:30 +02002220
Liron Kuch72b78552012-10-30 17:47:50 +02002221 /* save user defined memory free function for later use */
2222 channel->memfree = memfree;
2223 channel->user_info = user;
2224
Hamad Kadmany090709b2013-01-06 12:08:13 +02002225 /*
2226 * For small buffers, create a DMA pool so that memory
2227 * is not wasted through dma_alloc_coherent.
2228 */
2229 if (TSPP_USE_DMA_POOL(channel->buffer_size)) {
2230 channel->dma_pool = dma_pool_create("tspp",
2231 NULL, channel->buffer_size, 0, 0);
2232 if (!channel->dma_pool) {
2233 pr_err("%s: Can't allocate memory pool\n", __func__);
2234 return -ENOMEM;
2235 }
2236 } else {
2237 channel->dma_pool = NULL;
2238 }
2239
2240
Liron Kuch72b78552012-10-30 17:47:50 +02002241 for (channel->buffer_count = 0;
2242 channel->buffer_count < channel->max_buffers;
Joel Nider435ad8e2011-12-14 16:53:30 +02002243 channel->buffer_count++) {
2244
2245 /* allocate the descriptor */
2246 struct tspp_mem_buffer *desc = (struct tspp_mem_buffer *)
2247 kmalloc(sizeof(struct tspp_mem_buffer), GFP_KERNEL);
2248 if (!desc) {
Liron Kuch72b78552012-10-30 17:47:50 +02002249 pr_warn("%s: Can't allocate desc %i",
2250 __func__, channel->buffer_count);
Joel Nider435ad8e2011-12-14 16:53:30 +02002251 break;
2252 }
2253
2254 desc->desc.id = channel->buffer_count;
2255 /* allocate the buffer */
2256 if (tspp_alloc_buffer(channel_id, &desc->desc,
Hamad Kadmany090709b2013-01-06 12:08:13 +02002257 channel->buffer_size, channel->dma_pool,
2258 alloc, user) != 0) {
Joel Nider435ad8e2011-12-14 16:53:30 +02002259 kfree(desc);
Liron Kuch72b78552012-10-30 17:47:50 +02002260 pr_warn("%s: Can't allocate buffer %i",
2261 __func__, channel->buffer_count);
Joel Nider435ad8e2011-12-14 16:53:30 +02002262 break;
2263 }
2264
2265 /* add the descriptor to the list */
2266 desc->filled = 0;
2267 desc->read_index = 0;
2268 if (!channel->data) {
2269 channel->data = desc;
2270 desc->next = channel->data;
2271 } else {
2272 last->next = desc;
2273 }
2274 last = desc;
2275 desc->next = channel->data;
2276
2277 /* prepare the sps descriptor */
2278 desc->sps.phys_base = desc->desc.phys_base;
2279 desc->sps.base = desc->desc.virt_base;
2280 desc->sps.size = desc->desc.size;
2281
2282 /* start the transfer */
2283 if (tspp_queue_buffer(channel, desc))
Liron Kuch72b78552012-10-30 17:47:50 +02002284 pr_err("%s: can't queue buffer %i",
2285 __func__, desc->desc.id);
2286 }
2287
2288 if (channel->buffer_count < channel->max_buffers) {
2289 /*
2290 * we failed to allocate the requested number of buffers.
2291 * we don't allow a partial success, so need to clean up here.
2292 */
2293 tspp_destroy_buffers(channel_id, channel);
2294 channel->buffer_count = 0;
Hamad Kadmany090709b2013-01-06 12:08:13 +02002295
2296 if (channel->dma_pool) {
2297 dma_pool_destroy(channel->dma_pool);
2298 channel->dma_pool = NULL;
2299 }
Liron Kuch72b78552012-10-30 17:47:50 +02002300 return -ENOMEM;
Joel Nider435ad8e2011-12-14 16:53:30 +02002301 }
2302
2303 channel->waiting = channel->data;
2304 channel->read = channel->data;
2305 channel->locked = channel->data;
Liron Kuch72b78552012-10-30 17:47:50 +02002306
Hamad Kadmany81cee052012-11-29 14:15:57 +02002307 /* Now that buffers are scheduled to HW, kick data expiration timer */
2308 if (channel->expiration_period_ms)
2309 mod_timer(&channel->expiration_timer,
2310 jiffies +
2311 MSEC_TO_JIFFIES(
2312 channel->expiration_period_ms));
2313
Joel Nider435ad8e2011-12-14 16:53:30 +02002314 return 0;
2315}
2316EXPORT_SYMBOL(tspp_allocate_buffers);
Joel Nider5556a852011-10-16 10:52:13 +02002317
2318/*** File Operations ***/
2319static ssize_t tspp_open(struct inode *inode, struct file *filp)
2320{
Joel Nider435ad8e2011-12-14 16:53:30 +02002321 u32 dev;
Joel Nider5556a852011-10-16 10:52:13 +02002322 struct tspp_channel *channel;
Joel Nider435ad8e2011-12-14 16:53:30 +02002323
2324 TSPP_DEBUG("tspp_open");
Joel Nider5556a852011-10-16 10:52:13 +02002325 channel = container_of(inode->i_cdev, struct tspp_channel, cdev);
2326 filp->private_data = channel;
Joel Nider435ad8e2011-12-14 16:53:30 +02002327 dev = channel->pdev->pdev->id;
Joel Nider5556a852011-10-16 10:52:13 +02002328
2329 /* if this channel is already in use, quit */
2330 if (channel->used) {
2331 pr_err("tspp channel %i already in use",
2332 MINOR(channel->cdev.dev));
2333 return -EACCES;
2334 }
2335
Joel Nider435ad8e2011-12-14 16:53:30 +02002336 if (tspp_open_channel(dev, channel->id) != 0) {
Joel Nider5556a852011-10-16 10:52:13 +02002337 pr_err("tspp: error opening channel");
2338 return -EACCES;
2339 }
2340
2341 return 0;
2342}
2343
2344static unsigned int tspp_poll(struct file *filp, struct poll_table_struct *p)
2345{
2346 unsigned long flags;
2347 unsigned int mask = 0;
2348 struct tspp_channel *channel;
2349 channel = filp->private_data;
2350
2351 /* register the wait queue for this channel */
2352 poll_wait(filp, &channel->in_queue, p);
2353
2354 spin_lock_irqsave(&channel->pdev->spinlock, flags);
Joel Nider435ad8e2011-12-14 16:53:30 +02002355 if (channel->read &&
2356 channel->read->state == TSPP_BUF_STATE_DATA)
Joel Nider5556a852011-10-16 10:52:13 +02002357 mask = POLLIN | POLLRDNORM;
2358
2359 spin_unlock_irqrestore(&channel->pdev->spinlock, flags);
2360
2361 return mask;
2362}
2363
2364static ssize_t tspp_release(struct inode *inode, struct file *filp)
2365{
Joel Nider435ad8e2011-12-14 16:53:30 +02002366 struct tspp_channel *channel = filp->private_data;
2367 u32 dev = channel->pdev->pdev->id;
2368 TSPP_DEBUG("tspp_release");
Joel Nider5556a852011-10-16 10:52:13 +02002369
Joel Nider435ad8e2011-12-14 16:53:30 +02002370 tspp_close_channel(dev, channel->id);
Joel Nider5556a852011-10-16 10:52:13 +02002371
2372 return 0;
2373}
2374
2375static ssize_t tspp_read(struct file *filp, char __user *buf, size_t count,
2376 loff_t *f_pos)
2377{
2378 size_t size = 0;
2379 size_t transferred = 0;
2380 struct tspp_channel *channel;
2381 struct tspp_mem_buffer *buffer;
2382 channel = filp->private_data;
2383
2384 TSPP_DEBUG("tspp_read");
Joel Nider435ad8e2011-12-14 16:53:30 +02002385
2386 while (!channel->read) {
2387 if (filp->f_flags & O_NONBLOCK) {
2388 pr_warn("tspp: no buffer on channel %i!",
2389 channel->id);
2390 return -EAGAIN;
2391 }
2392 /* go to sleep if there is nothing to read */
2393 if (wait_event_interruptible(channel->in_queue,
2394 (channel->read != NULL))) {
2395 pr_err("tspp: rude awakening\n");
2396 return -ERESTARTSYS;
2397 }
2398 }
2399
2400 buffer = channel->read;
2401
Joel Nider5556a852011-10-16 10:52:13 +02002402 /* see if we have any buffers ready to read */
2403 while (buffer->state != TSPP_BUF_STATE_DATA) {
2404 if (filp->f_flags & O_NONBLOCK) {
2405 pr_warn("tspp: nothing to read on channel %i!",
2406 channel->id);
2407 return -EAGAIN;
2408 }
2409 /* go to sleep if there is nothing to read */
Joel Nider5556a852011-10-16 10:52:13 +02002410 if (wait_event_interruptible(channel->in_queue,
2411 (buffer->state == TSPP_BUF_STATE_DATA))) {
2412 pr_err("tspp: rude awakening\n");
2413 return -ERESTARTSYS;
2414 }
2415 }
2416
2417 while (buffer->state == TSPP_BUF_STATE_DATA) {
2418 size = min(count, buffer->filled);
Joel Nider5556a852011-10-16 10:52:13 +02002419 if (size == 0)
2420 break;
2421
Joel Nider435ad8e2011-12-14 16:53:30 +02002422 if (copy_to_user(buf, buffer->desc.virt_base +
Joel Nider5556a852011-10-16 10:52:13 +02002423 buffer->read_index, size)) {
2424 pr_err("tspp: error copying to user buffer");
Joel Nider435ad8e2011-12-14 16:53:30 +02002425 return -EBUSY;
Joel Nider5556a852011-10-16 10:52:13 +02002426 }
2427 buf += size;
2428 count -= size;
2429 transferred += size;
2430 buffer->read_index += size;
2431
Liron Kuch72b78552012-10-30 17:47:50 +02002432 /*
2433 * after reading the end of the buffer, requeue it,
2434 * and set up for reading the next one
2435 */
Joel Nider435ad8e2011-12-14 16:53:30 +02002436 if (buffer->read_index == buffer->filled) {
Joel Nider5556a852011-10-16 10:52:13 +02002437 buffer->state = TSPP_BUF_STATE_WAITING;
Hamad Kadmany090709b2013-01-06 12:08:13 +02002438
Joel Nider435ad8e2011-12-14 16:53:30 +02002439 if (tspp_queue_buffer(channel, buffer))
2440 pr_err("tspp: can't submit transfer");
Hamad Kadmany090709b2013-01-06 12:08:13 +02002441
Joel Nider435ad8e2011-12-14 16:53:30 +02002442 channel->locked = channel->read;
2443 channel->read = channel->read->next;
Joel Nider5556a852011-10-16 10:52:13 +02002444 }
2445 }
2446
2447 return transferred;
2448}
2449
2450static long tspp_ioctl(struct file *filp,
2451 unsigned int param0, unsigned long param1)
2452{
Joel Nider435ad8e2011-12-14 16:53:30 +02002453 u32 dev;
Joel Nider5556a852011-10-16 10:52:13 +02002454 int rc = -1;
2455 struct tspp_channel *channel;
Joel Nider435ad8e2011-12-14 16:53:30 +02002456 struct tspp_select_source ss;
2457 struct tspp_filter f;
2458 struct tspp_key k;
2459 struct tspp_iv iv;
2460 struct tspp_system_keys sk;
2461 struct tspp_buffer b;
Joel Nider5556a852011-10-16 10:52:13 +02002462 channel = filp->private_data;
Joel Nider435ad8e2011-12-14 16:53:30 +02002463 dev = channel->pdev->pdev->id;
Joel Nider5556a852011-10-16 10:52:13 +02002464
Liron Kucha7b49ae2013-02-14 16:26:38 +02002465 if ((param0 != TSPP_IOCTL_CLOSE_STREAM) && !param1)
Joel Nider5556a852011-10-16 10:52:13 +02002466 return -EINVAL;
2467
2468 switch (param0) {
2469 case TSPP_IOCTL_SELECT_SOURCE:
Joel Nider435ad8e2011-12-14 16:53:30 +02002470 if (!access_ok(VERIFY_READ, param1,
2471 sizeof(struct tspp_select_source))) {
2472 return -EBUSY;
2473 }
2474 if (__copy_from_user(&ss, (void *)param1,
2475 sizeof(struct tspp_select_source)) == 0)
2476 rc = tspp_select_source(dev, channel->id, &ss);
Joel Nider5556a852011-10-16 10:52:13 +02002477 break;
2478 case TSPP_IOCTL_ADD_FILTER:
Joel Nider435ad8e2011-12-14 16:53:30 +02002479 if (!access_ok(VERIFY_READ, param1,
2480 sizeof(struct tspp_filter))) {
2481 return -ENOSR;
2482 }
2483 if (__copy_from_user(&f, (void *)param1,
2484 sizeof(struct tspp_filter)) == 0)
2485 rc = tspp_add_filter(dev, channel->id, &f);
Joel Nider5556a852011-10-16 10:52:13 +02002486 break;
2487 case TSPP_IOCTL_REMOVE_FILTER:
Joel Nider435ad8e2011-12-14 16:53:30 +02002488 if (!access_ok(VERIFY_READ, param1,
2489 sizeof(struct tspp_filter))) {
2490 return -EBUSY;
2491 }
2492 if (__copy_from_user(&f, (void *)param1,
2493 sizeof(struct tspp_filter)) == 0)
2494 rc = tspp_remove_filter(dev, channel->id, &f);
Joel Nider5556a852011-10-16 10:52:13 +02002495 break;
2496 case TSPP_IOCTL_SET_KEY:
Joel Nider435ad8e2011-12-14 16:53:30 +02002497 if (!access_ok(VERIFY_READ, param1,
2498 sizeof(struct tspp_key))) {
2499 return -EBUSY;
2500 }
2501 if (__copy_from_user(&k, (void *)param1,
2502 sizeof(struct tspp_key)) == 0)
2503 rc = tspp_set_key(dev, channel->id, &k);
Joel Nider5556a852011-10-16 10:52:13 +02002504 break;
2505 case TSPP_IOCTL_SET_IV:
Joel Nider435ad8e2011-12-14 16:53:30 +02002506 if (!access_ok(VERIFY_READ, param1,
2507 sizeof(struct tspp_iv))) {
2508 return -EBUSY;
2509 }
2510 if (__copy_from_user(&iv, (void *)param1,
2511 sizeof(struct tspp_iv)) == 0)
2512 rc = tspp_set_iv(channel, &iv);
Joel Nider5556a852011-10-16 10:52:13 +02002513 break;
2514 case TSPP_IOCTL_SET_SYSTEM_KEYS:
Joel Nider435ad8e2011-12-14 16:53:30 +02002515 if (!access_ok(VERIFY_READ, param1,
2516 sizeof(struct tspp_system_keys))) {
2517 return -EINVAL;
2518 }
2519 if (__copy_from_user(&sk, (void *)param1,
2520 sizeof(struct tspp_system_keys)) == 0)
2521 rc = tspp_set_system_keys(channel, &sk);
Joel Nider5556a852011-10-16 10:52:13 +02002522 break;
2523 case TSPP_IOCTL_BUFFER_SIZE:
Joel Nider435ad8e2011-12-14 16:53:30 +02002524 if (!access_ok(VERIFY_READ, param1,
2525 sizeof(struct tspp_buffer))) {
2526 rc = -EINVAL;
2527 }
2528 if (__copy_from_user(&b, (void *)param1,
2529 sizeof(struct tspp_buffer)) == 0)
2530 rc = tspp_set_buffer_size(channel, &b);
Joel Nider5556a852011-10-16 10:52:13 +02002531 break;
Liron Kucha7b49ae2013-02-14 16:26:38 +02002532 case TSPP_IOCTL_CLOSE_STREAM:
2533 rc = tspp_close_stream(dev, channel->id);
2534 break;
Joel Nider5556a852011-10-16 10:52:13 +02002535 default:
2536 pr_err("tspp: Unknown ioctl %i", param0);
2537 }
2538
Liron Kuch72b78552012-10-30 17:47:50 +02002539 /*
2540 * normalize the return code in case one of the subfunctions does
2541 * something weird
2542 */
Joel Nider5556a852011-10-16 10:52:13 +02002543 if (rc != 0)
Joel Nider435ad8e2011-12-14 16:53:30 +02002544 rc = -ENOIOCTLCMD;
Joel Nider5556a852011-10-16 10:52:13 +02002545
2546 return rc;
2547}
2548
2549/*** debugfs ***/
Joel Nider5556a852011-10-16 10:52:13 +02002550static int debugfs_iomem_x32_set(void *data, u64 val)
2551{
2552 writel_relaxed(val, data);
2553 wmb();
2554 return 0;
2555}
2556
2557static int debugfs_iomem_x32_get(void *data, u64 *val)
2558{
2559 *val = readl_relaxed(data);
2560 return 0;
2561}
2562
2563DEFINE_SIMPLE_ATTRIBUTE(fops_iomem_x32, debugfs_iomem_x32_get,
2564 debugfs_iomem_x32_set, "0x%08llx");
2565
2566static void tsif_debugfs_init(struct tspp_tsif_device *tsif_device,
2567 int instance)
2568{
2569 char name[10];
2570 snprintf(name, 10, "tsif%i", instance);
2571 tsif_device->dent_tsif = debugfs_create_dir(
2572 name, NULL);
2573 if (tsif_device->dent_tsif) {
2574 int i;
2575 void __iomem *base = tsif_device->base;
2576 for (i = 0; i < ARRAY_SIZE(debugfs_tsif_regs); i++) {
2577 tsif_device->debugfs_tsif_regs[i] =
2578 debugfs_create_file(
2579 debugfs_tsif_regs[i].name,
2580 debugfs_tsif_regs[i].mode,
2581 tsif_device->dent_tsif,
2582 base + debugfs_tsif_regs[i].offset,
2583 &fops_iomem_x32);
2584 }
Hamad Kadmany44307d32012-11-25 09:49:51 +02002585
2586 debugfs_create_u32(
2587 "stat_rx_chunks",
Hamad Kadmanya1dde822013-03-28 08:23:26 +02002588 S_IRUGO | S_IWUSR | S_IWGRP,
Hamad Kadmany44307d32012-11-25 09:49:51 +02002589 tsif_device->dent_tsif,
2590 &tsif_device->stat_rx);
2591
2592 debugfs_create_u32(
2593 "stat_overflow",
Hamad Kadmanya1dde822013-03-28 08:23:26 +02002594 S_IRUGO | S_IWUSR | S_IWGRP,
Hamad Kadmany44307d32012-11-25 09:49:51 +02002595 tsif_device->dent_tsif,
2596 &tsif_device->stat_overflow);
2597
2598 debugfs_create_u32(
2599 "stat_lost_sync",
Hamad Kadmanya1dde822013-03-28 08:23:26 +02002600 S_IRUGO | S_IWUSR | S_IWGRP,
Hamad Kadmany44307d32012-11-25 09:49:51 +02002601 tsif_device->dent_tsif,
2602 &tsif_device->stat_lost_sync);
2603
2604 debugfs_create_u32(
2605 "stat_timeout",
Hamad Kadmanya1dde822013-03-28 08:23:26 +02002606 S_IRUGO | S_IWUSR | S_IWGRP,
Hamad Kadmany44307d32012-11-25 09:49:51 +02002607 tsif_device->dent_tsif,
2608 &tsif_device->stat_timeout);
Joel Nider5556a852011-10-16 10:52:13 +02002609 }
2610}
2611
2612static void tsif_debugfs_exit(struct tspp_tsif_device *tsif_device)
2613{
2614 if (tsif_device->dent_tsif) {
2615 int i;
2616 debugfs_remove_recursive(tsif_device->dent_tsif);
2617 tsif_device->dent_tsif = NULL;
2618 for (i = 0; i < ARRAY_SIZE(debugfs_tsif_regs); i++)
2619 tsif_device->debugfs_tsif_regs[i] = NULL;
2620 }
2621}
2622
2623static void tspp_debugfs_init(struct tspp_device *device, int instance)
2624{
2625 char name[10];
2626 snprintf(name, 10, "tspp%i", instance);
2627 device->dent = debugfs_create_dir(
2628 name, NULL);
2629 if (device->dent) {
2630 int i;
2631 void __iomem *base = device->base;
2632 for (i = 0; i < ARRAY_SIZE(debugfs_tspp_regs); i++) {
2633 device->debugfs_regs[i] =
2634 debugfs_create_file(
2635 debugfs_tspp_regs[i].name,
2636 debugfs_tspp_regs[i].mode,
2637 device->dent,
2638 base + debugfs_tspp_regs[i].offset,
2639 &fops_iomem_x32);
2640 }
2641 }
2642}
2643
2644static void tspp_debugfs_exit(struct tspp_device *device)
2645{
2646 if (device->dent) {
2647 int i;
2648 debugfs_remove_recursive(device->dent);
2649 device->dent = NULL;
2650 for (i = 0; i < ARRAY_SIZE(debugfs_tspp_regs); i++)
2651 device->debugfs_regs[i] = NULL;
2652 }
2653}
Joel Nider5556a852011-10-16 10:52:13 +02002654
Liron Kuch59339922013-01-01 18:29:47 +02002655/* copy device-tree data to platfrom data struct */
2656static __devinit struct msm_tspp_platform_data *
2657msm_tspp_dt_to_pdata(struct platform_device *pdev)
2658{
2659 struct device_node *node = pdev->dev.of_node;
2660 struct msm_tspp_platform_data *data;
2661 struct msm_gpio *gpios;
2662 int i, rc;
2663 int gpio;
2664 u32 gpio_func;
2665
2666 /* Note: memory allocated by devm_kzalloc is freed automatically */
2667 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
2668 if (!data) {
2669 pr_err("tspp: Unable to allocate platform data\n");
2670 return NULL;
2671 }
2672 rc = of_property_read_string(node, "qcom,tsif-pclk", &data->tsif_pclk);
2673 if (rc) {
2674 pr_err("tspp: Could not find tsif-pclk property, err = %d\n",
2675 rc);
2676 return NULL;
2677 }
2678 rc = of_property_read_string(node, "qcom,tsif-ref-clk",
2679 &data->tsif_ref_clk);
2680 if (rc) {
2681 pr_err("tspp: Could not find tsif-ref-clk property, err = %d\n",
2682 rc);
2683 return NULL;
2684 }
2685
2686 data->num_gpios = of_gpio_count(node);
2687 if (data->num_gpios == 0) {
2688 pr_err("tspp: Could not find GPIO definitions\n");
2689 return NULL;
2690 }
2691 gpios = devm_kzalloc(&pdev->dev,
2692 (data->num_gpios * sizeof(struct msm_gpio)),
2693 GFP_KERNEL);
2694 if (!gpios) {
2695 pr_err("tspp: Unable to allocate memory for GPIOs table\n");
2696 return NULL;
2697 }
2698 /* Assuming GPIO FUNC property is the same for all GPIOs */
2699 if (of_property_read_u32(node, "qcom,gpios-func", &gpio_func)) {
2700 pr_err("tspp: Could not find gpios-func property\n");
2701 return NULL;
2702 }
2703 for (i = 0; i < data->num_gpios; i++) {
2704 gpio = of_get_gpio(node, i);
2705 gpios[i].gpio_cfg = GPIO_CFG(gpio, gpio_func,
2706 GPIO_CFG_INPUT,
2707 GPIO_CFG_PULL_DOWN,
2708 GPIO_CFG_2MA);
2709 rc = of_property_read_string_index(node, "qcom,gpio-names",
2710 i, &gpios[i].label);
2711 if (rc)
2712 pr_warn("tspp: Could not find gpio-names property\n");
2713 }
2714
2715 data->gpios = gpios;
2716
2717 return data;
2718}
2719
2720static int msm_tspp_map_irqs(struct platform_device *pdev,
2721 struct tspp_device *device)
2722{
2723 int rc;
2724 int i;
2725
2726 /* get IRQ numbers from platform information */
2727
2728 /* map TSPP IRQ */
2729 rc = platform_get_irq_byname(pdev, "TSIF_TSPP_IRQ");
2730 if (rc > 0) {
2731 device->tspp_irq = rc;
2732 rc = request_irq(device->tspp_irq, tspp_isr, IRQF_SHARED,
2733 dev_name(&pdev->dev), device);
2734 if (rc) {
2735 dev_err(&pdev->dev,
2736 "failed to request TSPP IRQ %d : %d",
2737 device->tspp_irq, rc);
2738 device->tspp_irq = 0;
2739 return -EINVAL;
2740 }
2741 } else {
2742 dev_err(&pdev->dev, "failed to get TSPP IRQ");
2743 return -EINVAL;
2744 }
2745
2746 /* map TSIF IRQs */
2747 rc = platform_get_irq_byname(pdev, "TSIF0_IRQ");
2748 if (rc > 0) {
2749 device->tsif[0].tsif_irq = rc;
2750 } else {
2751 dev_err(&pdev->dev, "failed to get TSIF0 IRQ");
2752 return -EINVAL;
2753 }
2754
2755 rc = platform_get_irq_byname(pdev, "TSIF1_IRQ");
2756 if (rc > 0) {
2757 device->tsif[1].tsif_irq = rc;
2758 } else {
2759 dev_err(&pdev->dev, "failed to get TSIF1 IRQ");
2760 return -EINVAL;
2761 }
2762
2763 for (i = 0; i < TSPP_TSIF_INSTANCES; i++) {
2764 rc = request_irq(device->tsif[i].tsif_irq,
2765 tsif_isr, IRQF_SHARED,
2766 dev_name(&pdev->dev), &device->tsif[i]);
2767 if (rc) {
2768 dev_warn(&pdev->dev, "failed to request TSIF%d IRQ: %d",
2769 i, rc);
2770 device->tsif[i].tsif_irq = 0;
2771 }
2772 }
2773
2774 /* map BAM IRQ */
2775 rc = platform_get_irq_byname(pdev, "TSIF_BAM_IRQ");
2776 if (rc > 0) {
2777 device->bam_irq = rc;
2778 } else {
2779 dev_err(&pdev->dev, "failed to get TSPP BAM IRQ");
2780 return -EINVAL;
2781 }
2782
2783 return 0;
2784}
2785
Joel Nider5556a852011-10-16 10:52:13 +02002786static int __devinit msm_tspp_probe(struct platform_device *pdev)
2787{
2788 int rc = -ENODEV;
2789 u32 version;
Liron Kuch72b78552012-10-30 17:47:50 +02002790 u32 i, j;
Joel Nider5556a852011-10-16 10:52:13 +02002791 struct msm_tspp_platform_data *data;
2792 struct tspp_device *device;
2793 struct resource *mem_tsif0;
2794 struct resource *mem_tsif1;
2795 struct resource *mem_tspp;
2796 struct resource *mem_bam;
Liron Kuch72b78552012-10-30 17:47:50 +02002797 struct tspp_channel *channel;
Joel Nider5556a852011-10-16 10:52:13 +02002798
Liron Kuch59339922013-01-01 18:29:47 +02002799 if (pdev->dev.of_node) {
2800 /* get information from device tree */
2801 data = msm_tspp_dt_to_pdata(pdev);
2802 /* get device ID */
2803 rc = of_property_read_u32(pdev->dev.of_node,
2804 "cell-index", &pdev->id);
2805 if (rc)
2806 pdev->id = -1;
2807
2808 pdev->dev.platform_data = data;
2809 } else {
2810 /* must have platform data */
2811 data = pdev->dev.platform_data;
2812 }
Joel Nider5556a852011-10-16 10:52:13 +02002813 if (!data) {
2814 pr_err("tspp: Platform data not available");
2815 rc = -EINVAL;
2816 goto out;
2817 }
2818
2819 /* check for valid device id */
Joel Nider435ad8e2011-12-14 16:53:30 +02002820 if ((pdev->id < 0) || (pdev->id >= TSPP_MAX_DEVICES)) {
Joel Nider5556a852011-10-16 10:52:13 +02002821 pr_err("tspp: Invalid device ID %d", pdev->id);
2822 rc = -EINVAL;
2823 goto out;
2824 }
2825
2826 /* OK, we will use this device */
2827 device = kzalloc(sizeof(struct tspp_device), GFP_KERNEL);
2828 if (!device) {
2829 pr_err("tspp: Failed to allocate memory for device");
2830 rc = -ENOMEM;
2831 goto out;
2832 }
2833
2834 /* set up references */
2835 device->pdev = pdev;
2836 platform_set_drvdata(pdev, device);
2837
2838 /* map clocks */
2839 if (data->tsif_pclk) {
Joel Niderb9662ca2012-06-10 14:21:11 +03002840 device->tsif_pclk = clk_get(&pdev->dev, data->tsif_pclk);
Joel Nider5556a852011-10-16 10:52:13 +02002841 if (IS_ERR(device->tsif_pclk)) {
2842 pr_err("tspp: failed to get %s",
2843 data->tsif_pclk);
2844 rc = PTR_ERR(device->tsif_pclk);
2845 device->tsif_pclk = NULL;
2846 goto err_pclock;
2847 }
2848 }
2849 if (data->tsif_ref_clk) {
Joel Niderb9662ca2012-06-10 14:21:11 +03002850 device->tsif_ref_clk = clk_get(&pdev->dev, data->tsif_ref_clk);
Joel Nider5556a852011-10-16 10:52:13 +02002851 if (IS_ERR(device->tsif_ref_clk)) {
2852 pr_err("tspp: failed to get %s",
2853 data->tsif_ref_clk);
2854 rc = PTR_ERR(device->tsif_ref_clk);
2855 device->tsif_ref_clk = NULL;
2856 goto err_refclock;
2857 }
2858 }
2859
2860 /* map I/O memory */
Liron Kuch59339922013-01-01 18:29:47 +02002861 mem_tsif0 = platform_get_resource_byname(pdev,
2862 IORESOURCE_MEM, "MSM_TSIF0_PHYS");
Joel Nider5556a852011-10-16 10:52:13 +02002863 if (!mem_tsif0) {
2864 pr_err("tspp: Missing tsif0 MEM resource");
2865 rc = -ENXIO;
2866 goto err_res_tsif0;
2867 }
2868 device->tsif[0].base = ioremap(mem_tsif0->start,
2869 resource_size(mem_tsif0));
2870 if (!device->tsif[0].base) {
2871 pr_err("tspp: ioremap failed");
2872 goto err_map_tsif0;
2873 }
2874
Liron Kuch59339922013-01-01 18:29:47 +02002875 mem_tsif1 = platform_get_resource_byname(pdev,
2876 IORESOURCE_MEM, "MSM_TSIF1_PHYS");
Joel Nider5556a852011-10-16 10:52:13 +02002877 if (!mem_tsif1) {
2878 dev_err(&pdev->dev, "Missing tsif1 MEM resource");
2879 rc = -ENXIO;
2880 goto err_res_tsif1;
2881 }
2882 device->tsif[1].base = ioremap(mem_tsif1->start,
2883 resource_size(mem_tsif1));
2884 if (!device->tsif[1].base) {
2885 dev_err(&pdev->dev, "ioremap failed");
2886 goto err_map_tsif1;
2887 }
2888
Liron Kuch59339922013-01-01 18:29:47 +02002889 mem_tspp = platform_get_resource_byname(pdev,
2890 IORESOURCE_MEM, "MSM_TSPP_PHYS");
Joel Nider5556a852011-10-16 10:52:13 +02002891 if (!mem_tspp) {
2892 dev_err(&pdev->dev, "Missing MEM resource");
2893 rc = -ENXIO;
2894 goto err_res_dev;
2895 }
2896 device->base = ioremap(mem_tspp->start, resource_size(mem_tspp));
2897 if (!device->base) {
2898 dev_err(&pdev->dev, "ioremap failed");
2899 goto err_map_dev;
2900 }
2901
Liron Kuch59339922013-01-01 18:29:47 +02002902 mem_bam = platform_get_resource_byname(pdev,
2903 IORESOURCE_MEM, "MSM_TSPP_BAM_PHYS");
Joel Nider5556a852011-10-16 10:52:13 +02002904 if (!mem_bam) {
2905 pr_err("tspp: Missing bam MEM resource");
2906 rc = -ENXIO;
2907 goto err_res_bam;
2908 }
2909 memset(&device->bam_props, 0, sizeof(device->bam_props));
2910 device->bam_props.phys_addr = mem_bam->start;
2911 device->bam_props.virt_addr = ioremap(mem_bam->start,
2912 resource_size(mem_bam));
2913 if (!device->bam_props.virt_addr) {
2914 dev_err(&pdev->dev, "ioremap failed");
2915 goto err_map_bam;
2916 }
2917
Liron Kuch59339922013-01-01 18:29:47 +02002918 if (msm_tspp_map_irqs(pdev, device))
Joel Nider5556a852011-10-16 10:52:13 +02002919 goto err_irq;
Joel Nider5556a852011-10-16 10:52:13 +02002920
Joel Nider5556a852011-10-16 10:52:13 +02002921 /* power management */
2922 pm_runtime_set_active(&pdev->dev);
2923 pm_runtime_enable(&pdev->dev);
2924
Joel Nider5556a852011-10-16 10:52:13 +02002925 tspp_debugfs_init(device, 0);
2926
2927 for (i = 0; i < TSPP_TSIF_INSTANCES; i++)
2928 tsif_debugfs_init(&device->tsif[i], i);
Joel Nider5556a852011-10-16 10:52:13 +02002929
2930 wake_lock_init(&device->wake_lock, WAKE_LOCK_SUSPEND,
2931 dev_name(&pdev->dev));
2932
2933 /* set up pointers to ram-based 'registers' */
Joel Nider435ad8e2011-12-14 16:53:30 +02002934 device->filters[0] = device->base + TSPP_PID_FILTER_TABLE0;
2935 device->filters[1] = device->base + TSPP_PID_FILTER_TABLE1;
2936 device->filters[2] = device->base + TSPP_PID_FILTER_TABLE2;
2937 device->tspp_key_table = device->base + TSPP_DATA_KEY;
2938 device->tspp_global_performance =
2939 device->base + TSPP_GLOBAL_PERFORMANCE;
2940 device->tspp_pipe_context =
2941 device->base + TSPP_PIPE_CONTEXT;
2942 device->tspp_pipe_performance =
2943 device->base + TSPP_PIPE_PERFORMANCE;
Joel Nider5556a852011-10-16 10:52:13 +02002944
2945 device->bam_props.summing_threshold = 0x10;
2946 device->bam_props.irq = device->bam_irq;
2947 device->bam_props.manage = SPS_BAM_MGR_LOCAL;
2948
Liron Kuch59339922013-01-01 18:29:47 +02002949 if (tspp_clock_start(device) != 0) {
2950 dev_err(&pdev->dev, "Can't start clocks");
2951 goto err_clock;
2952 }
2953
Joel Nider5556a852011-10-16 10:52:13 +02002954 if (sps_register_bam_device(&device->bam_props,
2955 &device->bam_handle) != 0) {
2956 pr_err("tspp: failed to register bam");
2957 goto err_bam;
2958 }
2959
Joel Nider5556a852011-10-16 10:52:13 +02002960 spin_lock_init(&device->spinlock);
2961 tasklet_init(&device->tlet, tspp_sps_complete_tlet,
2962 (unsigned long)device);
2963
2964 /* initialize everything to a known state */
2965 tspp_global_reset(device);
2966
2967 version = readl_relaxed(device->base + TSPP_VERSION);
Liron Kuch59339922013-01-01 18:29:47 +02002968 /*
2969 * TSPP version can be bits [7:0] or alternatively,
2970 * TSPP major version is bits [31:28].
2971 */
2972 if ((version != 0x1) && (((version >> 28) & 0xF) != 0x1))
Joel Nider5556a852011-10-16 10:52:13 +02002973 pr_warn("tspp: unrecognized hw version=%i", version);
2974
Joel Nider435ad8e2011-12-14 16:53:30 +02002975 /* initialize the channels */
Joel Nider5556a852011-10-16 10:52:13 +02002976 for (i = 0; i < TSPP_NUM_CHANNELS; i++) {
Joel Nider435ad8e2011-12-14 16:53:30 +02002977 if (tspp_channel_init(&(device->channels[i]), device) != 0) {
2978 pr_err("tspp_channel_init failed");
2979 goto err_channel;
2980 }
Joel Nider5556a852011-10-16 10:52:13 +02002981 }
2982
Joel Nider435ad8e2011-12-14 16:53:30 +02002983 /* stop the clocks for power savings */
2984 tspp_clock_stop(device);
2985
2986 /* everything is ok, so add the device to the list */
2987 list_add_tail(&(device->devlist), &tspp_devices);
2988
Joel Nider5556a852011-10-16 10:52:13 +02002989 return 0;
2990
Joel Nider435ad8e2011-12-14 16:53:30 +02002991err_channel:
Liron Kuch59339922013-01-01 18:29:47 +02002992 /* un-initialize channels */
Liron Kuch72b78552012-10-30 17:47:50 +02002993 for (j = 0; j < i; j++) {
2994 channel = &(device->channels[i]);
2995 device_destroy(tspp_class, channel->cdev.dev);
2996 cdev_del(&channel->cdev);
2997 }
Liron Kuch59339922013-01-01 18:29:47 +02002998
Joel Nider5556a852011-10-16 10:52:13 +02002999 sps_deregister_bam_device(device->bam_handle);
Liron Kuch59339922013-01-01 18:29:47 +02003000err_clock:
Joel Nider5556a852011-10-16 10:52:13 +02003001err_bam:
Joel Nider5556a852011-10-16 10:52:13 +02003002 tspp_debugfs_exit(device);
3003 for (i = 0; i < TSPP_TSIF_INSTANCES; i++)
3004 tsif_debugfs_exit(&device->tsif[i]);
Joel Nider5556a852011-10-16 10:52:13 +02003005err_irq:
Liron Kuch59339922013-01-01 18:29:47 +02003006 for (i = 0; i < TSPP_TSIF_INSTANCES; i++) {
3007 if (device->tsif[i].tsif_irq)
3008 free_irq(device->tsif[i].tsif_irq, &device->tsif[i]);
3009 }
3010 if (device->tspp_irq)
3011 free_irq(device->tspp_irq, device);
3012
Joel Nider5556a852011-10-16 10:52:13 +02003013 iounmap(device->bam_props.virt_addr);
3014err_map_bam:
3015err_res_bam:
3016 iounmap(device->base);
3017err_map_dev:
3018err_res_dev:
3019 iounmap(device->tsif[1].base);
3020err_map_tsif1:
3021err_res_tsif1:
3022 iounmap(device->tsif[0].base);
3023err_map_tsif0:
3024err_res_tsif0:
3025 if (device->tsif_ref_clk)
3026 clk_put(device->tsif_ref_clk);
3027err_refclock:
3028 if (device->tsif_pclk)
3029 clk_put(device->tsif_pclk);
3030err_pclock:
3031 kfree(device);
3032
3033out:
3034 return rc;
3035}
3036
3037static int __devexit msm_tspp_remove(struct platform_device *pdev)
3038{
Joel Nider435ad8e2011-12-14 16:53:30 +02003039 struct tspp_channel *channel;
Joel Nider5556a852011-10-16 10:52:13 +02003040 u32 i;
Liron Kuch605cc122013-02-21 14:25:57 +02003041 int rc;
Joel Nider5556a852011-10-16 10:52:13 +02003042
3043 struct tspp_device *device = platform_get_drvdata(pdev);
3044
Joel Nider435ad8e2011-12-14 16:53:30 +02003045 /* free the buffers, and delete the channels */
3046 for (i = 0; i < TSPP_NUM_CHANNELS; i++) {
3047 channel = &device->channels[i];
3048 tspp_close_channel(device->pdev->id, i);
3049 device_destroy(tspp_class, channel->cdev.dev);
3050 cdev_del(&channel->cdev);
3051 }
3052
Liron Kuch59339922013-01-01 18:29:47 +02003053 /* de-registering BAM device requires clocks */
Liron Kuch605cc122013-02-21 14:25:57 +02003054 rc = tspp_clock_start(device);
3055 if (rc == 0) {
3056 sps_deregister_bam_device(device->bam_handle);
3057 tspp_clock_stop(device);
3058 }
Joel Nider5556a852011-10-16 10:52:13 +02003059
Hamad Kadmany44307d32012-11-25 09:49:51 +02003060 for (i = 0; i < TSPP_TSIF_INSTANCES; i++) {
Joel Nider5556a852011-10-16 10:52:13 +02003061 tsif_debugfs_exit(&device->tsif[i]);
Hamad Kadmany44307d32012-11-25 09:49:51 +02003062 if (device->tsif[i].tsif_irq)
3063 free_irq(device->tsif[i].tsif_irq, &device->tsif[i]);
3064 }
Joel Nider5556a852011-10-16 10:52:13 +02003065
3066 wake_lock_destroy(&device->wake_lock);
3067 free_irq(device->tspp_irq, device);
Joel Nider5556a852011-10-16 10:52:13 +02003068
3069 iounmap(device->bam_props.virt_addr);
3070 iounmap(device->base);
3071 for (i = 0; i < TSPP_TSIF_INSTANCES; i++)
3072 iounmap(device->tsif[i].base);
3073
3074 if (device->tsif_ref_clk)
3075 clk_put(device->tsif_ref_clk);
3076
3077 if (device->tsif_pclk)
3078 clk_put(device->tsif_pclk);
3079
3080 pm_runtime_disable(&pdev->dev);
Liron Kuch605cc122013-02-21 14:25:57 +02003081
Joel Nider5556a852011-10-16 10:52:13 +02003082 kfree(device);
3083
3084 return 0;
3085}
3086
3087/*** power management ***/
3088
3089static int tspp_runtime_suspend(struct device *dev)
3090{
3091 dev_dbg(dev, "pm_runtime: suspending...");
3092 return 0;
3093}
3094
3095static int tspp_runtime_resume(struct device *dev)
3096{
3097 dev_dbg(dev, "pm_runtime: resuming...");
3098 return 0;
3099}
3100
3101static const struct dev_pm_ops tspp_dev_pm_ops = {
3102 .runtime_suspend = tspp_runtime_suspend,
3103 .runtime_resume = tspp_runtime_resume,
3104};
3105
Liron Kuch59339922013-01-01 18:29:47 +02003106static struct of_device_id msm_match_table[] = {
3107 {.compatible = "qcom,msm_tspp"},
3108 {}
3109};
3110
Joel Nider5556a852011-10-16 10:52:13 +02003111static struct platform_driver msm_tspp_driver = {
3112 .probe = msm_tspp_probe,
3113 .remove = __exit_p(msm_tspp_remove),
3114 .driver = {
3115 .name = "msm_tspp",
3116 .pm = &tspp_dev_pm_ops,
Liron Kuch59339922013-01-01 18:29:47 +02003117 .of_match_table = msm_match_table,
Joel Nider5556a852011-10-16 10:52:13 +02003118 },
3119};
3120
3121
3122static int __init mod_init(void)
3123{
Joel Nider5556a852011-10-16 10:52:13 +02003124 int rc;
3125
Joel Nider435ad8e2011-12-14 16:53:30 +02003126 /* make the char devs (channels) */
Joel Nider5556a852011-10-16 10:52:13 +02003127 rc = alloc_chrdev_region(&tspp_minor, 0, TSPP_NUM_CHANNELS, "tspp");
3128 if (rc) {
3129 pr_err("tspp: alloc_chrdev_region failed: %d", rc);
3130 goto err_devrgn;
3131 }
3132
3133 tspp_class = class_create(THIS_MODULE, "tspp");
3134 if (IS_ERR(tspp_class)) {
3135 rc = PTR_ERR(tspp_class);
3136 pr_err("tspp: Error creating class: %d", rc);
3137 goto err_class;
3138 }
3139
Joel Nider435ad8e2011-12-14 16:53:30 +02003140 /* register the driver, and check hardware */
3141 rc = platform_driver_register(&msm_tspp_driver);
3142 if (rc) {
3143 pr_err("tspp: platform_driver_register failed: %d", rc);
3144 goto err_register;
Joel Nider5556a852011-10-16 10:52:13 +02003145 }
3146
3147 return 0;
3148
Joel Nider435ad8e2011-12-14 16:53:30 +02003149err_register:
3150 class_destroy(tspp_class);
Joel Nider5556a852011-10-16 10:52:13 +02003151err_class:
3152 unregister_chrdev_region(0, TSPP_NUM_CHANNELS);
3153err_devrgn:
Joel Nider5556a852011-10-16 10:52:13 +02003154 return rc;
3155}
3156
3157static void __exit mod_exit(void)
3158{
Joel Nider435ad8e2011-12-14 16:53:30 +02003159 /* delete low level driver */
3160 platform_driver_unregister(&msm_tspp_driver);
Joel Nider5556a852011-10-16 10:52:13 +02003161
Joel Nider435ad8e2011-12-14 16:53:30 +02003162 /* delete upper layer interface */
Joel Nider5556a852011-10-16 10:52:13 +02003163 class_destroy(tspp_class);
3164 unregister_chrdev_region(0, TSPP_NUM_CHANNELS);
Joel Nider5556a852011-10-16 10:52:13 +02003165}
3166
3167module_init(mod_init);
3168module_exit(mod_exit);
3169
Joel Nider435ad8e2011-12-14 16:53:30 +02003170MODULE_DESCRIPTION("TSPP platform device and char dev");
Joel Nider5556a852011-10-16 10:52:13 +02003171MODULE_LICENSE("GPL v2");